时间:2021-07-01 10:21:17 帮助过:8人阅读
本文实例讲述了asp.net中MVC借助Iframe实现无刷新上传文件的方法。分享给大家供大家参考。具体实现方法如下:
html:
代码如下:<div id="uploadwindow" style="display: none;">
<form action="/ShopActivitys/ImportActivityItems" id="form1" name="form1" enctype="multipart/form-data" method="post" target="hidden_frame">
<table style="width: 300px;">
<tr>
<td style="width: 80px; display: block;">选择文件:</td>
<td>
<input type="file" id="file" name="activityitemsfile" />
</td>
</tr>
</table>
<input type="submit" value="上传文件" />
<iframe name='hidden_frame' id="hidden_frame" style='display: none'></iframe>
</form>
</div>
CallBack函数:
代码如下:var UploadFun = {
CallBack: function (msg) {
$.messager.alert('上传文件', msg, 'info');
}
};
后台处理:
代码如下:public void ImportActivityItems()
{
string error = "导入成功";
try
{
var f = Request.Files["activityitemsfile"];
var fpath = Server.MapPath("/Upload/活动链接数据/");
if (!Directory.Exists(fpath))
Directory.CreateDirectory(fpath);
string fullfilename = fpath + DateTime.Now.ToFileTime() + f.FileName;
f.SaveAs(fullfilename);
}
catch (Exception ex)
{
Logger.Error(ex.ToString());
error = "导入过程中发生错误,请重试";
}
Response.Write("<script type=\"text/javascript\"> parent.UploadFun.CallBack(\"" + error + "\")</script>");
}
希望本文所述对大家的asp.net程序设计有所帮助。