道理相通,我简单分享下在.net MVC下的实装。
1.制作Model类
{
//----------------------------------------------------------------
///
/// Import画面用
///
//----------------------------------------------------------------
public class tmp_UploadFile
{
///
public HttpPostedFileBase FileName { get; set; }
}
}
2.实装controller里的对应方法,我这个处理逻辑比较复杂,懒得修改了,反正就这个意思
if (uploadedFile != null && uploadedFile.ContentLength != 0)
{
string pathForSaving = Server.MapPath("~/AppData/Uploaded/");
try
{
if (BsnssBihin.IsExcel(uploadedFile.FileName))
{
path = System.IO.Path.Combine(pathForSaving,dateTimeNow + "" + uploadedFile.FileName);
uploadedFile.SaveAs(path);
isUploaded = BsnssBihin.UploadBihinChange(path,userName,ref uploadMsg);
if (isUploaded)
{
message = "アップロード成功しました!" + "\n" + uploadMsg;
Logger.Info("[成功]備品アップロード," + dateTimeNow + "," + "[" + userName + "]" + "[" + path + "]" + uploadMsg);
}
else
{
message = "アップロード失敗しました。" + "\n" + uploadMsg;
Logger.Info("[失敗]備品アップロード," + "[" + userName + "]" + "["+path + "]" + uploadMsg);
}
}
else
{
message = "ファイルの形式は不正です。";
}
}
catch (Exception ex)
{
message = string.Format("失敗しました: {0}",ex.Message);
Logger.Info("[失敗]備品アップロード: " + ex.Message + dateTimeNow + "," + "[" + userName + "]" + "[" + path + "]");
}
}
return Json(new { isUploaded = isUploaded,message = message },"text/html");
}
3.页面的实装