我需要通过流数据将文件从Web api下载到Windows应用程序。
Web api:
public HttpResponseMessage download(string fileid){
var response = new HttpResponseMessage ();
response.statuscode= HttpStatusCode.Ok;
response.Content =new StreamContent(filestream);// I have a filestream here .
return response;
在添加内容后添加的内容处置和内容类型。在客户端,我可以尝试以下操作。
HttpResponseMessage file = httpclinet.GetAsync("url").Result();
Var stream = file.Content.ReadAsStreamAsync().Result();
using (var data = File.create(@"somepath.txt"))
{
data.seek(0,seekorigin.begin);
stream.copyto(data);
}
但是我没有得到输出。得到的是流详细信息,例如stream.content,statuscode的版本对象。就像写了那个文件一样。如何将流数据写入此文件。
我建议您在此处使用async-await关键字,您可以像这样更改代码以获取结果:
HttpResponseMessage file = await httpclinet.GetAsync("url");
var stream = await file.Content.ReadAsStreamAsync()
此外,您还需要像这样将async
关键字添加到您的方法中:
public async Task<HttpResponseMessage> download(string fileid)