本文实例讲述了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>
<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');
CallBack: function (msg) {
$.messager.alert('上传文件', msg, 'info');
}
};
后台处理:
复制代码 代码如下:
public void ImportActivityItems()
{
string error = "导入成功";
try
{
var f = Request.Files["activityitemsfile"];
{
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程序设计有所帮助。