我正在使用ASP.NET Web API上传文件。我在RC之前这样做了,但由于某种原因,该文件被保存为“BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af”而不是文件名。下面的文件名变量也返回此bodypart字符串,而不是文件名。我似乎不知道我在哪里错了。任何帮助是赞赏。
客户代码:
function upload() { $("#divResult").html("Uploading..."); var formData = new FormData($('form')[0]); $.ajax({ url: 'api/files/uploadfile?folder=' + $('#ddlFolders').val(),type: 'POST',success: function (data) { $("#divResult").html(data); },data: formData,cache: false,contentType: false,processData: false }); };
控制器:
public Task<HttpResponseMessage> UploadFile([FromUri]string folder) { if (!Request.Content.IsMimeMultipartContent()) { throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType)); } // Save file MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(HttpContext.Current.Server.MapPath("~/Files")); Task<IEnumerable<HttpContent>> task = Request.Content.ReadAsMultipartAsync(provider); return task.ContinueWith<HttpResponseMessage>(contents => { string filename = provider.BodyPartFileNames.First().Value; return new HttpResponseMessage() { Content = new StringContent(string.Format("File saved in {0}.",folder)) }; },TaskScheduler.FromCurrentSynchronizationContext());
文件看起来像:
解决方法
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。