asp.net-mvc ASP.NET Web API 文件另存为“BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10970750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ASP.NET Web API File saved as "BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af"
提问by Rivka
I'm uploading files using the ASP.NET Web API. I've done this before the RC but for some reason the file is being saved as "BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af" instead of the file name. The filename variable below returns this bodypart string too instead of the file name. I can't seem to figure out where I'm going wrong. Any help is appreciated.
我正在使用 ASP.NET Web API 上传文件。我在 RC 之前已经这样做了,但由于某种原因,文件被保存为“BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af”而不是文件名。下面的文件名变量也返回此正文部分字符串而不是文件名。我似乎无法弄清楚我哪里出错了。任何帮助表示赞赏。
Client code:
客户端代码:
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
});
};
Controller:
控制器:
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());
The files are looking like:
这些文件看起来像:


回答by Henrik Frystyk Nielsen
That was a concious change we made -- it was considered a security risk to take the file name provided in the Content-Disposition header field and so instead we now compute a file name which is what you are seeing.
这是我们做出的一个有意识的更改——采用 Content-Disposition 标头字段中提供的文件名被认为存在安全风险,因此我们现在计算您所看到的文件名。
If you want to control the server local file name yourself then you can derive from MultipartFormDataStreamProvider and override GetLocalFileName to provide whatever name you want. Note though that there may be security considerations doing so.
如果您想自己控制服务器本地文件名,那么您可以从 MultipartFormDataStreamProvider 派生并覆盖 GetLocalFileName 以提供您想要的任何名称。但请注意,这样做可能存在安全考虑。
Hope this helps,
希望这可以帮助,
Henrik
亨里克
回答by Filip W
I updated the code for the tutorial to make it work with ASP.NET Web API RC. Indeed, as Henrik mentioned Content-Disposition is no longer used a file name. See the source files at the bottom of the post - http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/
我更新了本教程的代码,使其适用于 ASP.NET Web API RC。事实上,正如 Henrik 提到的 Content-Disposition 不再使用文件名。查看帖子底部的源文件 - http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/
Please note, that there are further changes to MultipartFormDataStreamProvider that didn't make the cut to the RC, so it's now even more flexible. Henrik blogged about those here - http://blogs.msdn.com/b/henrikn/archive/2012/04/27/asp-net-web-api-updates-april-27.aspx.
请注意,对 MultipartFormDataStreamProvider 的进一步更改并未削减 RC,因此它现在更加灵活。Henrik 在这里发表了关于这些的博客 - http://blogs.msdn.com/b/henrikn/archive/2012/04/27/asp-net-web-api-updates-april-27.aspx。
EDIT:I have blogged about new and improved way of uploading files in Web API RTM, so that should hopefully help gets things organized - http://www.strathweb.com/2012/08/a-guide-to-asynchronous-file-uploads-in-asp-net-web-api-rtm/
编辑:我在博客上写了关于在 Web API RTM 中上传文件的新方法和改进方法,因此希望这有助于组织事情 - http://www.strathweb.com/2012/08/a-guide-to-asynchronous-file -uploads-in-asp-net-web-api-rtm/
回答by VietNguyen
Here, this work for me
在这里,这对我有用
In API Controller
在 API 控制器中
// We implement MultipartFormDataStreamProvider to override the filename of File which
// will be stored on server, or else the default name will be of the format like Body-
// Part_{GUID}. In the following implementation we simply get the FileName from
// ContentDisposition Header of the Request Body.
public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
public CustomMultipartFormDataStreamProvider(string path) : base(path) { }
public override string GetLocalFileName(HttpContentHeaders headers)
{
return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
}
}
Then
然后
string root = HttpContext.Current.Server.MapPath("~/App_Data");
CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(root);
Thanks,
谢谢,

