C#WebAPI实现上传功能我爱你用代码怎么写
上传和下载是很常⽤的功能了,只有当⽤到的时候才发现不会写...,经过⼀番百度、筛选、整理修改后,实现了功能,下⾯简单的记录下实现⽅法。
⼀、上传功能
1.前端代码
上传⽂件 <input type="file" id="file" />
<input type="button" id="upload" value="上传⽂件" />
<script>
//上传
$("#upload").click(function () {
var formData = new FormData();
var file = ElementById("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]
5public string UploadFile()
6 {
7string result = string.Empty;
8try
9 {
10string uploadPath = HttpContext.Current.Server.MapPath("~/App_Data/");
11 HttpRequest request = System.Web.HttpContext.Current.Request;
12 HttpFileCollection fileCollection = request.Files;
13// 判断是否有⽂件
14if (fileCollection.Count > 0)
15 {
16// 获取⽂件
17 HttpPostedFile httpPostedFile = fileCollection[0];
18string fileExtension = Path.GetExtension(httpPostedFile.FileName);// ⽂件扩展名
19string fileName = Guid.NewGuid().ToString() + fileExtension;// 名称
20string filePath = uploadPath + httpPostedFile.FileName;// 上传路径
21// 如果⽬录不存在则要先创建
22if (!Directory.Exists(uploadPath))
23 {
24 Directory.CreateDirectory(uploadPath);
jquery下载超大文件25 }
26// 保存新的⽂件python汉字转unicode
27while (File.Exists(filePath))
28 {
29 fileName = Guid.NewGuid().ToString() + fileExtension;winform listview 局部刷新
30 filePath = uploadPath + fileName;
31 }
32 httpPostedFile.SaveAs(filePath);
33 result = "上传成功";
34 }
35 }
36catch (Exception)
37 {
38 result = "上传失败";
39 }
40return 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();
yvette英文名很土});
</script>
2.后台代码
1///<summary>
2///下载⽂件
3///</summary>
4 [HttpGet]
5public void DownloadFile()
6 {
7var request = HttpContext.Current.Request;
8 NameValueCollection nvCollection = request.Params;
9string fileName = nvCollection.GetValues("fileName")[0];sook
10string filePath = Path.Combine(HttpContext.Current.Server.MapPath("~/App_Data/"), fileName);
11if (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 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论