C# ASP MVC WebClient字节数组传输
我有一个webapi应用程序,它从另一个资源下载文件C# ASP MVC WebClient字节数组传输,c#,asp.net-mvc,webclient,C#,Asp.net Mvc,Webclient,我有一个webapi应用程序,它从另一个资源下载文件 public byte[] DownloadDocumentContent(int id) { WebClient client = new WebClient(); client.UseDefaultCredentials = true; var siteUrl = System.Configuration.ConfigurationManager.AppSettings["SiteUrl"]; return
public byte[] DownloadDocumentContent(int id)
{
WebClient client = new WebClient();
client.UseDefaultCredentials = true;
var siteUrl = System.Configuration.ConfigurationManager.AppSettings["SiteUrl"];
return client.DownloadData(siteUrl);
}
然后我在主机MVC应用程序中接收数据,如下所示
public byte[] DownloadDocumentContent()
{
WebClient client = new WebClient();
client.UseDefaultCredentials = true;
return client.DownloadData(webapiURL);
}
并将其作为文件发送给用户
public ActionResult DownloadDocument(string token)
{
//...
byte[] result = DownloadDocumentContent(readRequest.Document.Id);
return File(result, ContentType, string.Format("{0}.{1}", FileName, FileExtension));
}
问题是用户接收的文件内容无效。
在发送api应用程序之前,我检查了第一个字节-它是“80”
在MVC应用程序中收到后,它是“34”。我做错了什么?我要做的第一件事是使用fiddler或类似工具检查返回的信息,以确保web服务器没有以HTML格式发送错误消息。如果不是这种情况,您还可以看到返回的确切数据,这可能会为您提供出错的线索。我使用streamreader检查了数据,发现在发送之前,我有许多不同的符号,如“PK\u0003\u0004\u0014\0\u0…”,但在接收后,我有一些类似ASCII编码的字符串,如“UEsDBBQAB…”我有一个类似于DownloadDocument的操作,它返回一个FileResult(对于图像文件)而不是ActionResult。。。