在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
上传和下载是很常用的功能了,只有当用到的时候才发现不会写...,经过一番百度、筛选、整理修改后,实现了功能,下面简单的记录下实现方法。 一、上传功能 1.前端代码 上传文件 <input type="file" id="file" /> <input type="button" id="upload" value="上传文件" /> <script> //上传 $("#upload").click(function () { var formData = new FormData(); var file = document.getElementById("file").files[0]; formData.append("fileInfo", file); $.ajax({ url: "../api/File/UploadFile", type: "POST", data: formData, contentType: false,//必须false才会自动加上正确的Content-Type processData: false,//必须false才会避开jQuery对 formdata 的默认处理,XMLHttpRequest会对 formdata 进行正确的处理 success: function (data) { alert(data); }, error: function (data) { alert("上传失败!"); } }); }); </script> 2.后台代码 1 /// <summary> 2 /// 上传文件 3 /// </summary> 4 [HttpPost] 5 public string UploadFile() 6 { 7 string result = string.Empty; 8 try 9 { 10 string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data/"); 11 HttpRequest request = System.Web.HttpContext.Current.Request; 12 HttpFileCollection fileCollection = request.Files; 13 // 判断是否有文件 14 if (fileCollection.Count > 0) 15 { 16 // 获取文件 17 HttpPostedFile httpPostedFile = fileCollection[0]; 18 string fileExtension = Path.GetExtension(httpPostedFile.FileName);// 文件扩展名 19 string fileName = Guid.NewGuid().ToString() + fileExtension;// 名称 20 string filePath = uploadPath + httpPostedFile.FileName;// 上传路径 21 // 如果目录不存在则要先创建 22 if (!Directory.Exists(uploadPath)) 23 { 24 Directory.CreateDirectory(uploadPath); 25 } 26 // 保存新的文件 27 while (File.Exists(filePath)) 28 { 29 fileName = Guid.NewGuid().ToString() + fileExtension; 30 filePath = uploadPath + fileName; 31 } 32 httpPostedFile.SaveAs(filePath); 33 result = "上传成功"; 34 } 35 } 36 catch (Exception) 37 { 38 result = "上传失败"; 39 } 40 return result; 41 }
二、下载功能 1.前端代码 <form action="../api/File/DownloadFile" method="get" id="form"> 下载文件 <input type="text" id="name" name="fileName" value="222" /> </form> <input type="button" id="download" value="下载文件" /> <script> //下载 $("#download").click(function () { var form = $("#form"); form.submit(); }); </script> 2.后台代码 1 /// <summary> 2 /// 下载文件 3 /// </summary> 4 [HttpGet] 5 public void DownloadFile() 6 { 7 var request = HttpContext.Current.Request; 8 NameValueCollection nvCollection = request.Params; 9 string fileName = nvCollection.GetValues("fileName")[0]; 10 string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/"), fileName); 11 if (File.Exists(filePath)) 12 { 13 HttpResponse response = HttpContext.Current.Response; 14 response.Clear(); 15 response.ClearHeaders(); 16 response.ClearContent(); 17 response.Buffer = true; 18 response.AddHeader("content-disposition", string.Format("attachment; FileName={0}", fileName)); 19 response.Charset = "GB2312"; 20 response.ContentEncoding = Encoding.GetEncoding("GB2312"); 21 response.ContentType = MimeMapping.GetMimeMapping(fileName); 22 response.WriteFile(filePath); 23 response.Flush(); 24 response.Close(); 25 } 26 }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论