Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 没有MediaTypeFormatter可用于读取类型为';StreamContent';来自媒体类型为';应用程序/八位字节流';_C#_.net - Fatal编程技术网

C# 没有MediaTypeFormatter可用于读取类型为';StreamContent';来自媒体类型为';应用程序/八位字节流';

C# 没有MediaTypeFormatter可用于读取类型为';StreamContent';来自媒体类型为';应用程序/八位字节流';,c#,.net,C#,.net,我试图调用一个方法,该方法从win app返回asp.net web api中的文件,但出现以下错误: 没有MediaTypeFormatter可用于从媒体类型为“application/octet stream”的内容中读取“StreamContent”类型的对象 我已经检查了我的每一段代码,但我不知道如何解决这个问题 HttpClient _client = new HttpClient(); _client.BaseAddress = new Uri("http://localhost:4

我试图调用一个方法,该方法从win app返回asp.net web api中的文件,但出现以下错误:

没有MediaTypeFormatter可用于从媒体类型为“application/octet stream”的内容中读取“StreamContent”类型的对象

我已经检查了我的每一段代码,但我不知道如何解决这个问题

HttpClient _client = new HttpClient();
_client.BaseAddress = new Uri("http://localhost:49681/api/ZipFile/");
_client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/octet-stream"));

// _client.DefaultRequestHeaders.Accept.Add(new ContentDispositionHeaderValue("attachment"));
HttpResponseMessage resp = _client.GetAsync("DownloadFile?fileName=/" +  startPath).Result;
if (resp.StatusCode == System.Net.HttpStatusCode.OK)
{
    //var file = resp.Content.ReadAsAsync<string>().Result;
    var fileDownloaded = resp.Content.ReadAsAsync<StreamContent>().Result;

     Stream fileStream = File.Create(@"c:\test\" + fileDownloaded);
    //ZipFile.ExtractToDirectory(file, extractPath);
    //MessageBox.Show(resp);
}
else
{
    MessageBox.Show(resp.StatusCode.ToString());
}
HttpClient\u client=newhttpclient();
_client.BaseAddress=新Uri(“http://localhost:49681/api/ZipFile/");
_client.DefaultRequestHeaders.Accept.Add(新的MediaTypeWithQualityHeaderValue(“应用程序/八位字节流”);
//_client.DefaultRequestHeaders.Accept.Add(新的ContentDispositionHeaderValue(“附件”));
httpresponsemessageresp=_client.GetAsync(“DownloadFile?fileName=/”+startPath).Result;
if(resp.StatusCode==System.Net.HttpStatusCode.OK)
{
//var file=resp.Content.ReadAsAsync().Result;
var fileDownloaded=resp.Content.ReadAsAsync().Result;
Stream fileStream=File.Create(@“c:\test\”+fileDownloaded);
//提取器目录(文件,提取器路径);
//MessageBox.Show(resp);
}
其他的
{
Show(resp.StatusCode.ToString());
}

您不能使用
readasasasync
将HTTP响应的内容神奇地转换为
StreamContent
——您需要通过
MediaTypeFormatter
告诉系统如何进行转换(因为没有
ReadAsStreamContentAsync
)。但是,只需将内容作为字节数组(或使用ReadAsStreamAsync的流)读取,然后使用以下方法在磁盘上创建文件就容易多了:

var fileDownloaded = await response.Content.ReadAsByteArrayAsync();
using (FileStream fileStream = File.Open(@"c:\test\" + yourFileName, FileMode.OpenOrCreate))
{
    fileStream.Seek(0, SeekOrigin.End);
    await fileStream.WriteAsync(fileDownloaded, 0, fileDownloaded.Length);
}

您下载的文件类型可能重复<代码>应用程序/八位字节流意味着它们可以是任何东西。也许您可以使用
HttpContent.ReadAsStreamAsync
并将其分配给
fileStream
?我曾尝试使用此代码,但问题是“file.Length”。什么变量是“file”?您还需要确保正在等待调用的异步方法。问题中的代码没有这样做(_client.GetAsync),因此将不会返回正在进行的调用的最终结果。fileDownloaded.Length-我正在获取的任务不包含“Length”的定义。当我评论这行时,它将等待fileStream.WriteAsync(filedownload,0,filedownload.Length);我下载了我的文件,但读取0 ByTesse请参阅上面的评论。正在等待异步方法调用。如果您不等待呼叫,您将收到一个
任务
,而不是
字节[]
。我按照您的说明做了。我对此行有问题:wait fileStream.WriteAsync(filedownload,0,filedownload.Length);