Oli*_*ler 7 c# asp.net zip json asp.net-web-api
我想知道如何将zip文件发送到WebApi控制器,反之亦然.问题是我的WebApi使用json传输数据.zip文件不可序列化,也可以是流.字符串可以序列化.但是必须有另一种解决方案,而不是将zip转换为字符串而不是发送字符串.这听起来不对.
知道怎么做的吗?
如果您的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;
}
Run Code Online (Sandbox Code Playgroud)
当我发布HTTP表单时,这适用于我enctype="multipart/form-data".
| 归档时间: |
|
| 查看次数: |
10590 次 |
| 最近记录: |