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

前端 未结 3 1712
面向向阳花
面向向阳花 2020-12-08 06:55

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\

3条回答
  •  春和景丽
    2020-12-08 07:36

    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,

提交回复
热议问题