업로드,
public HttpResponseMessage Get()
{
var path = @"C:\Temp\file.zip";
var result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(path, FileMode.Open);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType =
new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "file.zip"
};
return result;
}
및 POST에 대한 다운로드 파일을 내부 얻을 수있는 StreamContent으로, 간단한 HttpResponseMessage를 사용해보십시오 그 스트림은 다음과 같습니다.
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;
}
enctype="multipart/form-data"
으로 HTTP 양식을 보내야합니다.
바이너리를 사용해야하는 경우 왜 JSON 만 허용하도록 제한하겠습니까? WebApi는 멀티 파트 또는 로우 바이트 스트림을 통해 바이너리를 읽는 여러 가지 방법을 지원합니다. –