小编Iry*_*yna的帖子

ASP.NET MVC 4 Web Api ajax文件上传

我正在使用asp.net mvc 4 web api开发某种服务.在一个表单上,用户必须上传少量文件,然后将表单提交给服务器.问题是在ajax文件上传到asp.net mvc web api.我已经实现了没有ajax的上传.但我需要用ajax完成它.这是实施

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);

    });
Run Code Online (Sandbox Code Playgroud)

我找到了使用HTML5 http://www.strathweb.com/2012/04/html5-drag-and-drop-asynchronous-multi-file-upload-with-asp-net-webapi/执行此操作的文章.我需要在IE8中完成这项工作.也许你有任何想法?

感谢任何帮助,Iryna.

asp.net-mvc jquery file-upload asp.net-mvc-4 asp.net-web-api

8
推荐指数
1
解决办法
2万
查看次数