上传文件MVC 4的Web API .NET 4 [英] Upload a file MVC 4 Web API .NET 4

查看:244
本文介绍了上传文件MVC 4的Web API .NET 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用MVC的是使用Visual Studio 2012的前preSS附带的版本。 (Microsoft.AspNet.Mvc.4.0.20710.0)

I'm using the version of MVC that shipped with Visual Studio 2012 express. (Microsoft.AspNet.Mvc.4.0.20710.0)

我想这是RTM版。

我发现很多的,所有使用该code例子在线:

I've found plenty of examples online which all use this code:

    public Task<HttpResponseMessage> PostFormData()
    {
        // Check if the request contains multipart/form-data.
        if (!Request.Content.IsMimeMultipartContent())
        {
            throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
        }

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

        // Read the form data and return an async task.
        var task = Request.Content.ReadAsMultipartAsync(provider).
            ContinueWith<HttpResponseMessage>(t =>
            {
                if (t.IsFaulted || t.IsCanceled)
                {
                    return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, t.Exception);
                }

                // This illustrates how to get the file names.
                foreach (MultipartFileData file in provider.FileData)
                {
                    Trace.WriteLine(file.Headers.ContentDisposition.FileName);
                    Trace.WriteLine("Server file path: " + file.LocalFileName);
                }
                return Request.CreateResponse(HttpStatusCode.OK);
            });

        return task;
    }

但是,这code总是continueWith结束,其中 t.IsFaulted ==真。例外情况如下:

意外的MIME多数据流的结束。 MIME多消息不是   完成了。

Unexpected end of MIME multipart stream. MIME multipart message is not complete.

下面是我的客户的形式。没有什么特别的,我想要做的jQuery的形式pluging为Ajax上传,但我甚至无法得到这样的工作。

Here is my client form. NOthing fancy, I want to do jquery form pluging for ajax upload, but I can't even get this way to work.

<form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" >
    <input type="file" />
    <input type="submit" value="Upload" />
</form>

我读过它是由解析器预期/ CR / LF在每封邮件的末尾,该错误已被固定在六月。

I've read that it is caused by the parser expecting /CR /LF at the end of each message, and that bug has been fixed in June.

我不明白是什么了,如果其真的不动,为什么不包含在此版本的MVC 4的?为什么在互联网上这样的例子很多吹捧,这code工作时,它没有在这个版本的MVC 4的?

What I cannot figure out is, if it was really fixed, why isn't it included this version of MVC 4? Why do so many examples on the internet tout that this code works when it does not in this version of MVC 4?

推荐答案

您缺少一个名称有关文件的属性输入

You are missing a name attribute on your file input.

<form name="uploadForm" method="post" enctype="multipart/form-data" action="api/upload" >
    <input name="myFile" type="file" />
    <input type="submit" value="Upload" />
</form>

输入不带它不会提交的浏览器。所以,你的FORMDATA是空导致 IsFaulted 被断言。

这篇关于上传文件MVC 4的Web API .NET 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆