ASP.NET MVC 4网页API AJAX文件上传 [英] ASP.NET MVC 4 Web Api ajax file upload

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

问题描述

我正在开发使用asp.net mvc的4个Web API某种服务。在一个形式的用户必须上传一些文件,然后提交表单服务器。问题是在阿贾克斯文件上传到ASP.NET MVC的Web API。我已经执行上传没有Ajax。但我需要它与阿贾克斯进行。
这是落实

I am developing some sort of service using asp.net mvc 4 web api. On one form user must upload few files and then submit form to server. Problem is in ajax file upload to asp.net mvc web api. I have already implemented upload without ajax. But i need it's done with ajax. This is implementation of

public Task<HttpResponseMessage> PostJob()
{
    if (!Request.Content.IsMimeMultipartContent())
    {
        throw new HttpResponseException(Request.CreateResponse(HttpStatusCode.UnsupportedMediaType));
    }

    string path = HttpContext.Current.Server.MapPath(string.Format("~/Resources/Documents"));
    MultipartFormDataStreamProvider provider = new MultipartFormDataStreamProvider(path);
    var request = Request.Content.ReadAsMultipartAsync(provider);

    var task = request.ContinueWith<HttpResponseMessage>(t =>
    {
        if (t.IsFaulted || t.IsCanceled)
        {
            return new HttpResponseMessage(HttpStatusCode.InternalServerError);
        }

        string fileName = provider.BodyPartFileNames.FirstOrDefault().Value;
        string originalName = provider.BodyPartFileNames.FirstOrDefault().Key.TrimStart('"').TrimEnd('"');
        string RandomName = provider.BodyPartFileNames.First().Value + Path.GetExtension(originalName);

        FileInfo file = new FileInfo(fileName);
        file.CopyTo(Path.Combine(path, originalName), true);
        file.Delete();


        return new HttpResponseMessage(HttpStatusCode.Created);

    });

我发现的文章,这是否使用HTML5 <一个href=\"http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/\">http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/.我需要在IE8这项工作。也许你有什么想法?

I have found article that does this using HTML5 http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/. I need this work in IE8. Maybe you have any ideas?

任何帮助是AP preciated,
伊莉娜。

Any help is appreciated, Iryna.

推荐答案

您不能上传在传统的浏览器,如IE8使用纯JavaScript的AJAX的文件。这样做的原因是,没有接入到由在文件输入用户选择的文件的内容。既然你没有访问到内容,则无法将其发送到服务器。

You cannot upload files using pure javascript with AJAX in legacy browsers such as IE8. The reason for this is that you don't have access to the file contents that was selected by the user in a file input. And since you don't have access to this contents, you cannot send it to the server.

您可以使用一些现有的文件上传插件:

You could use some of the existing file upload plugins:

  • Plupload
  • Blueimp
  • Valums Ajax Upload
  • Uploadify

它们将测试浏览器的功能,并且,如果它支持HTML5和新XHR2对象允许uuploading文件使用AJAX它将使用。或者,如果浏览器不支持的话,这个插件可以退回到Flash或隐藏的iframe的。所以,如果你需要支持旧版浏览器你没有太多的选择,但无论是使用一些其他的客户端脚本技术如Flash,或者使用一个隐藏的iframe其假货AJAX请求和实际发送一个正常的的multipart / form-data的的要求。

They will test the capabilities of the browser and if it supports HTML5 and the new XHR2 object which allows uuploading files with AJAX it will use that. Or if the browser doesn't support it, the plugin could fallback to either Flash or hidden iframes. So if you need to support legacy browsers you don't have much choice but either use some other client scripting technology such as Flash or use a hidden iframe which fakes the AJAX request and actually sends a normal multipart/form-data request.

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

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