Unexpected end of MIME multipart stream. MIME multipart message is not complete. Web API and superagent
Asked Answered
A

5

5

I have a problem with uploading files from client to web api. I am getting this error "Unexpected end of MIME multipart stream. MIME multipart message is not complete." in the controller when i am trying to read the multipart content. I ma building an React JS client with superagent and this is my code for the request:

 UploadFiles(files: File[]): Promise.IThenable<any> {

    return this.Post("/Payment/files" , {
        data: {
            files: files
        },
        headers: {
            "Content-Type": "multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p",
            "Content-Disposition": "form-data; name=Foo",
        }
    });
}

And this is my controller code:

[Route("files")]
[HttpPost]
public async Task<HttpResponseMessage> UploadFiles()
{
        string root = Path.GetTempPath();
        var provider = new MultipartFormDataStreamProvider(root);

        Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
        MemoryStream tempStream = new MemoryStream();
        reqStream.CopyTo(tempStream);

        tempStream.Seek(0, SeekOrigin.End);
        StreamWriter writer = new StreamWriter(tempStream);
        writer.WriteLine();
        writer.Flush();
        tempStream.Position = 0;

        StreamContent streamContent = new StreamContent(tempStream);
        foreach (var header in Request.Content.Headers)
        {
            streamContent.Headers.Add(header.Key, header.Value);

        }
        try
        {
            // Read the form data.
            streamContent.LoadIntoBufferAsync().Wait();

            //This is where it bugs out
            await streamContent.ReadAsMultipartAsync(provider);


            return Request.CreateResponse(HttpStatusCode.OK);
        }
        catch (Exception e)
        {
            return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, e);
        }

}
Allmon answered 24/8, 2016 at 13:33 Comment(0)
B
6

I think the issue here is caused by:

headers: {
             "Content-Type": "multipart/form-data; boundary=gc0p4Jq0M2Yt08jU534c0p",
             "Content-Disposition": "form-data; name=Foo",
         }

I had a similar issue and was solved by removing the header parameters, am guessing that superagent adds them automatically.

Banc answered 17/10, 2016 at 13:0 Comment(2)
In my case, I was using Postman and removing the headers from the request, solved the issue.Antibaryon
Don't know if it helps, just select the file in postman again, I copied the request, and the reference was gone.Fingering
A
1

I solved this problem by doing this:

 headers: {
                "Content-Disposition": "form-data; name=Foo",
                "enctype": "multipart/form-data"
          }
Allmon answered 12/1, 2017 at 15:11 Comment(0)
Q
1

I was receiving this error because I was disabling the FILE INPUT just before posting. If the FILE INPUT is disabled or if it does not have a name attribute, the file will not be uploaded and you will receive the "Unexpected end of MIME multipart stream. MIME multipart message is not complete" error message.

Don't do this:

<input type="file" disabled="disabled" id="myFile" />

Do this:

<input type="file" id="myFile" name="myFile" />
Queenhood answered 17/8, 2021 at 4:53 Comment(0)
A
0

I was able to solve this issue by removing the headers I set manually on my post method. Angular 6 + .NET Web Api

Annettaannette answered 21/5, 2018 at 7:41 Comment(0)
O
0

I got this error for a silly mistake: my browser agent/client (I was using Postman), did not actually have a file selected

Postman using Body to POST form-data but input control has null value -- "No file chosen"

If you're using Postman, also check your headers are appropriate (as seen in other answers for this question, and other questions):

Content-type field has been set as multipart/form-data

Outing answered 2/6, 2021 at 17:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.