学习日记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;
}

其他的文件上传也应该这样,因为机制都是差不多的

 

posted @ 2018-04-18 08:50  Zero77  阅读(3105)  评论(0编辑  收藏  举报