前台,form的target指向iframe
<form action="/EmailHandler.ashx?action=upload" id="form1" name="form1" enctype="multipart/form-data" method="post" target="hidden_frame"> <div class="up_file" id="up_file"> <input type="file" class="fl h_20 w_300" id="file" multiple=multiple name="upload" onchange="fileSelected()" /> <input type="submit" value="上传" /> </div> <iframe name=‘hidden_frame‘ id="hidden_frame" style=‘display:none‘></iframe> <div id="fileInfo"></div> </form>
//上传后的回调方法 function callback(msg) { document.getElementById("file").outerHTML = document.getElementById("file").outerHTML; $(‘#attr_url‘).val(msg); document.getElementById(‘fileInfo‘).innerHTML +="<p>上传成功</p>" }
后台:
case "upload": #region 上传文件 { string path = ""; if (context.Request.Files.Count > 0) { HttpFileCollection files = context.Request.Files;//接受文件 if (files.Count > 0) { foreach (string i in files) { HttpPostedFile file = files[i]; string fileName = Path.GetFileName(file.FileName);//获取文件名 string fileExt = Path.GetExtension(fileName);//获取文件类型 if (!Directory.Exists(context.Server.MapPath("/emailFiles/"))) { Directory.CreateDirectory(context.Server.MapPath("/emailFiles/")); } string dircStr = "/emailFiles/" + emp.uid + "/"; if (!Directory.Exists(context.Server.MapPath(dircStr))) { Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(dircStr))); } string name =dircStr+ DateTime.Now.Year+"-"+DateTime.Now.Month+"/"; if (!Directory.Exists(context.Server.MapPath(name))) { Directory.CreateDirectory(Path.GetDirectoryName(context.Server.MapPath(name))); } string fileLoadName = name +DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+ fileExt; file.SaveAs(context.Server.MapPath(fileLoadName)); path += fileLoadName + ‘,‘; } } path = path.TrimEnd(‘,‘); context.Response.Write("<script>parent.callback(‘" + path + "‘)</script>");//调用前台的回调方法 } break; } #endregion
时间: 2024-10-28 22:04:10