如何将zip文件发送到ASP.NET WebApi

如何将zip文件发送到ASP.NET WebApi,第1张

如何将zip文件发送到ASP.NET WebApi

如果您的API方法期望使用,

HttpRequestMessage
则可以从中提取流:

public HttpResponseMessage Put(HttpRequestMessage request){    var stream = GetStreamFromUploadedFile(request);    // do something with the stream, then return something}private static Stream GetStreamFromUploadedFile(HttpRequestMessage request){    // Awaiting these tasks in the usual manner was deadlocking the thread for some reason.    // So for now we're invoking a Task and explicitly creating a new thread.    // See here: http://stackoverflow.com/q/15201255/328193    IEnumerable<HttpContent> parts = null;    Task.Factory        .StartNew(() => parts = request.Content.ReadAsMultipartAsync().Result.Contents,  CancellationToken.None,  TaskCreationOptions.LongRunning,  TaskScheduler.Default)        .Wait();    Stream stream = null;    Task.Factory        .StartNew(() => stream = parts.First().ReadAsStreamAsync().Result,  CancellationToken.None,  TaskCreationOptions.LongRunning,  TaskScheduler.Default)        .Wait();    return stream;}

当使用张贴HTTP表单时,这对我有用

enctype="multipart/form-data"



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4978795.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-14
下一篇 2022-11-14

发表评论

登录后才能评论

评论列表(0条)

保存