本文實例講述了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程序設計有所幫助。