我怎样才能在上传的WebAPI一个分段文件流? [英] How can i get the stream for a Multipart file in webapi upload?

查看:706
本文介绍了我怎样才能在上传的WebAPI一个分段文件流?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候和感谢阅读我的文章!

我需要上传用流(天青Bl​​obstorage)的文件,只是不能找出如何让物体本身的流...见下文code:

我有点新来的的WebAPI,并使用了一些例子。我得到的文件和FILEDATA,但它不是正确的类型我的方法将其上传..所以..我需要得到或将其转换成普通流,这似乎..有点硬的时刻:)

我知道..我需要使用ReadAsStreamAsync()。结果以某种方式,但它在foreach循环中崩溃,因为我获得两个provider.Contents(第一个似乎是正确的,第二个......不是)。 ..

任何帮助将大大AP preciated ..

  [System.Web.Http.HttpPost]
    公共异步任务< Htt的presponseMessage>上传()
    {
        如果(!Request.Content.IsMimeMultipartContent())
        {
            this.Request.CreateResponse(的HTTPStatus code.UnsupportedMediaType);
        }        VAR提供商= GetMultipartProvider();
        VAR的结果=等待Request.Content.ReadAsMultipartAsync(供应商);        //在上传文件时给出如BodyPart_26d6abe1-3ae1-416a-9429-b35f15e6e5d5的通用名称
        //所以这是你如何能得到原始文件名
        变种originalFileName = GetDeserializedFileName(result.FileData.First());        // uploadedFileInfo对象会给你喜欢的文件长度一些额外的东西,
        //创建时间,目录名,几个文件系统的方法等。
        VAR uploadedFileInfo =新的FileInfo(result.FileData.First()LocalFileName);        //删除此行以及GetFormData方法,如果你不
        //你的上传请求发送任何形式的数据
        VAR fileUploadObj = GetFormData< UploadDataModel>(结果);        流FILESTREAM = NULL;        使用(流流=新的MemoryStream())
        {
            的foreach(HttpContent在provider.Contents内容)
            {
                BinaryFormatter的bFormatter =新的BinaryFormatter();
                bFormatter.Serialize(流content.ReadAsStreamAsync()的结果。);
                stream.Position = 0;
                FILESTREAM =流;
            }
        }        VAR存储=新StorageServices();
        storage.UploadBlob(FILESTREAM,originalFileName); **强大的文本**私人MultipartFormDataStreamProvider GetMultipartProvider()
    {
        VAR uploadFolder =〜/ App_Data文件/ tmp目录/文件上传; //你可以把这个web.config文件
        VAR根= HttpContext.Current.Server.MapPath(uploadFolder);
        Directory.CreateDirectory(根);
        返回新MultipartFormDataStreamProvider(根);
    }


解决方案

这是等同于一个两难我曾在几个月前(拍摄前的载流的 MultipartStreamProvider 接管并自动保存神奇的流文件)。该建议是继承类和重载的方法...但是,这并没有我的情况下工作。 :((我想双方的 MultipartFileStreamProvider MultipartFormDataStreamProvider 集于一身MultipartStreamProvider,没有自动保存部分功能)。

这可能会帮助; 这里有一个写的网页API开发人员之一,的这个来自同一个开发商。

Greetings and thanks for reading my post !

I need to upload a file using Stream (Azure Blobstorage), and just cannot find out how to get the stream from the object itself... See code below :

I'm kinda new to the WebAPI and have used some examples. I'm getting the files and filedata, but it's not correct type for my methods to upload it.. Therefore.. i need to get or convert it into a normal Stream, which.. seems a bit hard at the moment :)

I know.. i need to use ReadAsStreamAsync().Result in some way, but it crashes in the foreach loop since i'm getting two provider.Contents (first one seems right, second one... not)...

Any help would be greatly appreciated..

 [System.Web.Http.HttpPost]
    public async Task<HttpResponseMessage> Upload()
    {
        if (!Request.Content.IsMimeMultipartContent())
        {
            this.Request.CreateResponse(HttpStatusCode.UnsupportedMediaType);
        }

        var provider = GetMultipartProvider();
        var result = await Request.Content.ReadAsMultipartAsync(provider);

        // On upload, files are given a generic name like "BodyPart_26d6abe1-3ae1-416a-9429-b35f15e6e5d5"
        // so this is how you can get the original file name
        var originalFileName = GetDeserializedFileName(result.FileData.First());

        // uploadedFileInfo object will give you some additional stuff like file length,
        // creation time, directory name, a few filesystem methods etc..
        var uploadedFileInfo = new FileInfo(result.FileData.First().LocalFileName);



        // Remove this line as well as GetFormData method if you're not
        // sending any form data with your upload request
        var fileUploadObj = GetFormData<UploadDataModel>(result);

        Stream filestream = null;

        using (Stream stream = new MemoryStream())
        {
            foreach (HttpContent content in provider.Contents)
            {
                BinaryFormatter bFormatter = new BinaryFormatter();
                bFormatter.Serialize(stream, content.ReadAsStreamAsync().Result);
                stream.Position = 0;
                filestream = stream;
            }
        }

        var storage = new StorageServices();
        storage.UploadBlob(filestream, originalFileName);**strong text**



private MultipartFormDataStreamProvider GetMultipartProvider()
    {
        var uploadFolder = "~/App_Data/Tmp/FileUploads"; // you could put this to web.config
        var root = HttpContext.Current.Server.MapPath(uploadFolder);
        Directory.CreateDirectory(root);
        return new MultipartFormDataStreamProvider(root);
    }

解决方案

This is identical to a dilemma I had a few months ago (capturing the upload stream before the MultipartStreamProvider took over and auto-magically saved the stream to a file). The recommendation was to inherit that class and override the methods ... but that didn't work in my case. :( (I wanted the functionality of both the MultipartFileStreamProvider and MultipartFormDataStreamProvider rolled into one MultipartStreamProvider, without the autosave part).

This might help; here's one written by one of the Web API developers, and this from the same developer.

这篇关于我怎样才能在上传的WebAPI一个分段文件流?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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