WebApi FormData+文件长传 异步+同步实现
2018-10-17 23:48
387 查看
// POST api/values public async Task Post() { try { // 检查该请求是否含有multipart/form-data if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType); } // 方法一 System.Web.HttpFileCollection file = System.Web.HttpContext.Current.Request.Files; if (file.Count > 0) { //文件名 string name = file[0].FileName; //保存文件 string path = HttpContext.Current.Server.MapPath("~/") + name; file[0].SaveAs(path); } Dictionary<string, string> dicFormData = new Dictionary<string, string>(); foreach (var key in System.Web.HttpContext.Current.Request.Form.AllKeys) { //接收FormData dicFormData.Add(key, System.Web.HttpContext.Current.Request.Form[key]); } // 方法二 Dictionary<string, string> dic = new Dictionary<string, string>(); string root = HttpContext.Current.Server.MapPath("~/App_Data");//指定要将文件存入的服务器物理位置 var provider = new MultipartFormDataStreamProvider(root); // Read the form data. await Request.Content.ReadAsMultipartAsync(provider); // This illustrates how to get the file names. foreach (MultipartFileData file1 in provider.FileData) {//接收文件 Trace.WriteLine(file1.Headers.ContentDisposition.FileName);//获取上传文件实际的文件名 Trace.WriteLine("Server file path: " + file1.LocalFileName);//获取上传文件在服务上默认的文件名 } foreach (var key in provider.FormData.AllKeys) {//接收FormData dic.Add(key, provider.FormData[key]); } } catch (Exception ex) { throw ex; } }
相关文章推荐
- javascript 使用FormData实现图片/文件异步上传
- FormData可实现异步传输二进制文件(即异步文件上传)
- 利用html5的FormData对象和ajax实现异步文件上传
- 用xmlhttp将html的数据打包成multipart/form-data格式,实现异步上传文件功能[转]
- 用xmlhttp将html的数据打包成multipart/form-data格式,实现异步上传文件功能
- 利用html5的FormData对象和ajax实现异步文件上传
- 用FormData实现无刷新页面异步上传文件
- 使用H5的formData对象实现文件的异步上传
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- 使用jQuery.form插件实现表单异步提交+上传文件
- NSURLConnection和NSMutableURLRequest 实现同步、异步请求 和 NSInputStream和NSMutableURLRequest-实现保存文件到服务器
- 使用PHP和HTML5 FormData实现无刷新文件上传教程
- [转载红鱼儿]Delphi实现微信开发(3)如何使用multipart/form-data格式上传文件
- ASP.NET 异步Web API + jQuery Ajax 文件上传代码小析
- 使用PHP和HTML5 FormData实现无刷新文件上传教程
- "Asp.Net Web Api MediaTypeFormatter Error for x-www-formurlencoded data" 解决方法
- asp.net/c# 用<input type="file" />实现文件上传,multipart/form-data
- webpy+uploadify实现文件异步上传
- Android文件图片上传的详细讲解(一)HTTP multipart/form-data 上传报文格式实现手机端上传
- jQuery利用XMLHttpRequest()和FormData()实现同时上传文件和数据