MultipartFormDataStreamProvider的问题

时间:2012-08-21 18:21:17

标签: asp.net-mvc asp.net-web-api

我正在尝试关注this tutorial,我意识到即使我复制并通过他的代码,我的ApiController中也会遇到两个编译错误。

IEnumerable<HttpContent> bodyparts = Request.Content.ReadAsMultipartAsync(streamProvider);

这告诉我,ReadAsMultipartAsync的返回不能转换为HttpContent的IEnumerable。

IDictionary<string, string> bodypartFiles = streamProvider.BodyPartFileNames;

这告诉我在StreamProvider中不存在BodyPartFileNames,这似乎与教程以及我见过的其他一些博客文章和StackOverflow问题相反。

任何人都知道这笔交易是什么?

完整档案:

using AsyncFileUpload.Models;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http;


namespace AsyncFileUpload.Controllers
{
    public class UploadingController : ApiController
    {
        private const string PATH = "C:\\_projects\\learning";

        [HttpPost]
        public async Task<IList<FileDesc>> Post()
        {
            List<FileDesc> result = new List<FileDesc>();
            if (Request.Content.IsMimeMultipartContent())
            {
                try
                {
                    if (!Directory.Exists(PATH))
                    {
                        Directory.CreateDirectory(PATH);
                    }

                    MultipartFormDataStreamProvider streamProvider =
                        new MultipartFormDataStreamProvider(PATH);

                    IEnumerable<HttpContent> bodyparts = Request.Content.ReadAsMultipartAsync(streamProvider);
                    IDictionary<string, string> bodypartFiles = streamProvider.BodyPartFileNames;
                    IList<string> newFiles = new List<string>();

                    foreach (var item in bodypartFiles)
                    {
                        var newName = string.Empty;
                        var file = new FileInfo(item.Value);

                        if (item.Key.Contains("\""))
                        {
                            newName = Path.Combine(file.Directory.ToString(),
                                item.Key.Substring(1, item.Key.Length - 2));
                        }
                        else
                        {
                            newName = Path.Combine(file.Directory.ToString(), item.Key);
                        }

                        File.Move(file.FullName, newName);
                        newFiles.Add(newName);
                    }

                    var uploadedFiles = newFiles.Select(i =>
                        {
                            var fi = new FileInfo(i);
                            return new FileDesc(fi.Name, fi.FullName, fi.Length);
                        }).ToList();

                    result.AddRange(uploadedFiles);
                }
                catch (Exception e)
                {
                    // NOOP
                }
            }
            return result;
        }
    }
}

1 个答案:

答案 0 :(得分:16)

ReadAsMultipartAsync返回任务&lt;&gt;宾语。获取.Result属性(阻止)或使用await关键字等待任务(最好)。

在RTM版本中更改了BodyPartFileNames,现在使用FileData属性。

请参阅:http://www.asp.net/web-api/overview/working-with-http/sending-html-form-data,-part-2