學習日記18、easyui 文件框上傳文件


前台

<tr>
<td style="width:100px; text-align:right;">
@Html.LabelFor(model => model.ImgUrl):
</td>
<td style="width:310px">
@Html.HiddenFor(model => model.ImgUrl)
<input class="easyui-filebox" name="File" id="filed" data-options="prompt:'請選擇..'" style="width:200px;display:none" />
</td>
<td>@Html.ValidationMessageFor(model => model.ImgUrl)</td>
</tr>

 

前台提交的時候需要easyui   from表單的異步提交方法(別的提交表單方法后台獲取不到數據,不知道為什么)

提交表單代碼:

$("#btnSave").click(function () {
if ($("form").valid()) {
$("input[name='Content']").val(editor.getContent())
$("#ZxdFm").form('submit', {
datatype: 'json',
success: function (data) {
var json = eval("(" + data + ")");
if (json.type == 1) {
window.parent.frameReturnByMes(json.message);
window.parent.frameReturnByReload(true);
window.parent.frameReturnByClose()
}
else {
window.parent.frameReturnByMes(json.message);
}
}
})
}
return false;
});

這個返回回來的數據需要用eval解析一下,直接用獲取不到數據

public JsonResult Create(X_ZxDynamicInfoModel model,HttpPostedFileBase File)
{
model.Id = ResultHelper.NewId;
model.CreateTime = ResultHelper.NowTime;
if (File != null)
{
string imgurl = UploadFile(File);
if (imgurl == "1")
{
return Json(JsonHandler.CreateMessage(0, "請選擇正確的圖片格式"));
}
model.ImgUrl = imgurl;
}

}

后台接受文件代碼,UploadFile(HttpPostedFileBase  File)是我封裝的一個方法,返回的是一個圖片文件路徑

方法代碼如下

public string UploadFile(HttpPostedFileBase File)
{
string FileExtName = System.IO.Path.GetExtension(File.FileName).ToLower();
switch (FileExtName)
{
case ".jpg":
case ".jpeg":
case ".png":
case ".gif":
break;
default:
return "1";
}
//保存位置
string SaveFile = DateTime.Now.ToString("yyyyMMddHHmmssff");
//保存擴展名
SaveFile += System.IO.Path.GetExtension(File.FileName);
//生成完整的存儲路徑(網址)
string SavePathFile = string.Format("/Uploads/{0}", SaveFile);
string SavePathUrl = SavePathFile;//記錄保存的url路徑
//轉化生成磁盤物理路徑
SavePathFile = Server.MapPath(SavePathFile);
//開始保存
File.SaveAs(SavePathFile);
return SavePathUrl;
}

其他的文件上傳也應該這樣,因為機制都是差不多的

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM