ASP.NET Web API File saved as “BodyPart_3ded2bfb-40be-4183-b789-9301f93e90af”

女生的网名这么多〃 提交于 2019-11-28 05:17:44

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.

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.

Hope this helps,

Henrik

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/

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.

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/

Here, this work for me

In API Controller

// 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,

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!