码迷,mamicode.com
首页 > Windows程序 > 详细

webapi读取上传的文件流

时间:2019-04-24 23:33:47      阅读:730      评论:0      收藏:0      [点我收藏+]

标签:headers   tca   ssi   RKE   ev3   img   atm   zrm   dir   

逻辑说明

这里未引用System.Web.Mvc。
主要使用MultipartMemoryStreamProvider对象从Request中获取文件流。

var provider = new MultipartMemoryStreamProvider();
await Request.Content.ReadAsMultipartAsync(provider);
var items = provider.Contents.Where(c => c.Headers.ContentDisposition.FileName != null);

这里读取到的是所有带有文件名的http内容。

完整代码

扩展ApiController方法。

public static class ApiExtensions
{
    public async static Task<FormFileCollection> ReadFileCollectionAsync(this ApiController apiController)
    {
        var provider = new MultipartMemoryStreamProvider();
        await apiController.Request.Content.ReadAsMultipartAsync(provider);
        var items = provider.Contents.Where(c => c.Headers.ContentDisposition.FileName != null);
        FormFileCollection files = new FormFileCollection();
        foreach (var item in items)
        {
            var disposition = item.Headers.ContentDisposition;
            FormFile file = new FormFile(disposition.Name.Trim(‘"‘), disposition.FileName.Trim(‘"‘), item.Headers.ContentType.MediaType, await item.ReadAsStreamAsync(), item.Headers.ContentLength ?? 0);
            files.Add(file);
        }
        return files;
    }

    public static FormFileCollection ReadFileCollection(this ApiController apiController)
    {
        using (var t = ReadFileCollectionAsync(apiController))
        {
            t.Wait();
            return t.Result;
        }
    }
}

使用方法:
技术图片

public IHttpActionResult Upload()
{
    var dir = "/upload/file";
    var dirPath = HostingEnvironment.MapPath(dir);
    if (!Directory.Exists(dirPath))
    {
        Directory.CreateDirectory(dirPath);
    }
    var files = this.ReadFileCollection();
    var fileA = files["filea"];
    fileA.SaveAs(Path.Combine(dirPath, fileA.FileName));
    var fileD = files["filed"];
    fileD.SaveAs(Path.Combine(dirPath, fileD.FileName));
    return Ok();
}

FormFile类:

public class FormFile
{
    public FormFile(string name, string fileName, string contentType, Stream stream, long size)
    {
        this.Name = name;
        this.FileName = fileName;
        this.ContentType = contentType;
        this.Stream = stream;
        this.Size = size;
    }

    public string Name { get; private set; }
    public string FileName { get; private set; }
    public string ContentType { get; private set; }
    public long Size { get; private set; }
    public Stream Stream { get; private set; }

    public virtual void SaveAs(string filename)
    {
        using (var fileStream = new FileStream(filename, FileMode.Create))
        {
            byte[] buffer = new byte[4096];
            while (true)
            {
                int num = this.Stream.Read(buffer, 0, buffer.Length);
                if (num == 0)
                {
                    break;
                }
                fileStream.Write(buffer, 0, num);
            }
            fileStream.Flush();
        }
    }
}

FormFileCollection类:

public class FormFileCollection : IEnumerable<FormFile>
{
    readonly List<FormFile> files = new List<FormFile>();
    public int Count => files.Count;

    public FormFile this[int index]
    {
        get { return files[index]; }
    }

    public FormFile this[string index]
    {
        get { return files.Where(f => f.Name == index).FirstOrDefault(); }
    }

    public void Add(FormFile item)
    {
        files.Add(item);
    }

    public void AddRange(IEnumerable<FormFile> items)
    {
        files.AddRange(items);
    }

    public IEnumerator<FormFile> GetEnumerator()
    {
        return files.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

webapi读取上传的文件流

标签:headers   tca   ssi   RKE   ev3   img   atm   zrm   dir   

原文地址:https://www.cnblogs.com/junio/p/10765633.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!