流 获取multipartfile_如何在webapi上传中获取Multipart文件的流?

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 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 does not).

[System.Web.Http.HttpPost]

public async Task 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(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).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值