Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
735 views
in Technique[技术] by (71.8m points)

c# - WebAPI upload error. Expected end of MIME multipart stream. MIME multipart message is not complete

I've been following this tutorial to support file uploading as part of MVC4's WebAPI: http://blogs.msdn.com/b/henrikn/archive/2012/03/01/file-upload-and-asp-net-web-api.aspx.

When my controller receives the request, it complains that the 'MIME multipart message is not complete.'. Does anyone have any tips on how to debug this? I've tried resetting the stream's position to 0 on the off-chance there was something else reading it before it hit the handler.

My HTML looks like this:

<form action="/api/giggl" method="post" enctype="multipart/form-data">
    <span>Select file(s) to upload :</span>
    <input id="file1" type="file" multiple="multiple" />
    <input id="button1" type="submit" value="Upload" />
</form>

and my controller's Post method like this:

    public Task<IEnumerable<string>> Post()
    {
        if (Request.Content.IsMimeMultipartContent())
        {
            Stream reqStream = Request.Content.ReadAsStreamAsync().Result;
            if (reqStream.CanSeek)
            {
                reqStream.Position = 0;
            }

            string fullPath = HttpContext.Current.Server.MapPath("~/App_Data");
            var streamProvider = new MultipartFormDataStreamProvider(fullPath);

            var task = Request.Content.ReadAsMultipartAsync(streamProvider).ContinueWith(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                    Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);

                var fileInfo = streamProvider.FileData.Select(i =>
                {
                    var info = new FileInfo(i.LocalFileName);
                    return "File uploaded as " + info.FullName + " (" + info.Length + ")";
                });
                return fileInfo;

            });
            return task;
        }
        else
        {
            throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.NotAcceptable, "Invalid Request!"));
        }
    }

Am I missing anything obvious?>

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Can you try adding the "name" attribute to your input file?

<input name="file1" id="file1" type="file" multiple="multiple" />

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...