简体   繁体   中英

Uploading file with post from ASP.NET web api throws error when published to Azure but not localhost

I have this web api where I'm posting images to a folder in my web api and it works fine when doing it locally but when publishing the web api online it doesn't work and throws the following error message "Error writing MIME multipart body part to output stream". I've seen a few people with similar questions but i havent been able to solve it therefore im putting the code out there and hopefully someone could notice what may be causing this! here is the code:

 public async Task<HttpResponseMessage> Post()
    {
        // Check whether the POST operation is MultiPart?
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

        // Prepare CustomMultipartFormDataStreamProvider in which our multipart form
        // data will be loaded.
        string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data");
        CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);
        List<string> files = new List<string>();

        try
        {
            // Read all contents of multipart message into CustomMultipartFormDataStreamProvider.
            //Here the code goes down to public class CustomMultipartFormDataStreamProvider and runs that code and then jump back here. When it gets back here is when the Error is Thrown
            await Request.Content.ReadAsMultipartAsync(provider);

            foreach (MultipartFileData file in provider.FileData)
            {
                files.Add(Path.GetFileName(file.LocalFileName));
            }

            // Send OK Response along with saved file names to the client.
            return Request.CreateResponse(HttpStatusCode.OK, files);
        }
        catch (System.Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }
    }
}


public class CustomMultipartFormDataStreamProvider : MultipartFormDataStreamProvider
{
    public CustomMultipartFormDataStreamProvider(string path) : base(path) { }

    public override string GetLocalFileName(HttpContentHeaders headers)
    {
        return headers.ContentDisposition.FileName.Replace("\"", string.Empty);
    }
}

The code is from http://www.intstrings.com/ramivemula/articles/file-upload-using-multipartformdatastreamprovider-in-asp-net-webapi/ where you can read more about the code. Any help or input highly appreciated, thanks!

I am not sure you can store data in the App_Data. It will be a thousand times better if you store all your files in the Blob storage. If you need any help I will assist.

Follow this article and you will get it running.

https://azure.microsoft.com/en-us/documentation/articles/storage-dotnet-how-to-use-blobs/

Make sure the virtual directory ( ~/App_Data directory as below example) where the image files are first uploaded are physically existence. When you publish the project, it may not be in the output files.

string fileSaveLocation = HttpContext.Current.Server.MapPath("~/App_Data"); CustomMultipartFormDataStreamProvider provider = new CustomMultipartFormDataStreamProvider(fileSaveLocation);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM