C#文件下载已损坏 [英] C# File Download is Corrupt
问题描述
不幸的是,我不知道我做错了什么。实用程序的相关部分如下:
public static HttpResponseMessage StreamResponse(此HttpRequestMessage请求,流流)
{
if(stream.CanSeek)stream.Position = 0; //如果可能,重新设置流
HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
if(stream is FileStream)
{//如果这是一个FileStream,也可以弄清楚内容类型
string mimeType = MimeMapping.GetMimeMapping(((FileStream)stream).Name );
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType);
}
返回响应;
}
public static HttpResponseMessage DownloadAs(此HttpResponseMessage响应,string fileName)
{
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(attachment);
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(fileName));
return response; // for chaining or whatnot
}
我的用法API控制器是 return ResponseMessage(Request.StreamResponse(stream).DownloadAs(Filename.ext));
我已经仔细检查了代码下载,这似乎与我发现的匹配。 看起来像编码或字符集有问题,但我无法确定解决方案是什么。
最后,感谢本Q& A 。我在我的 $ http 呼叫客户端代码。
I've got some C# in a utility for a Web API project. The upload portion of the code works fine; I've verified the file that gets to the server matches the file that was uploaded. However, something is happening in the download that causes the client to view the file as corrupted, and when I do a diff I can see that something went wrong.
Unfortunately, I can't figure out what I'm doing wrong. The relevant parts of the utility are as follows:
public static HttpResponseMessage StreamResponse(this HttpRequestMessage request, Stream stream)
{
if (stream.CanSeek) stream.Position = 0;// Reset stream if possible
HttpResponseMessage response = request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(stream);
if (stream is FileStream)
{// If this is a FileStream, might as well figure out the content type
string mimeType = MimeMapping.GetMimeMapping(((FileStream)stream).Name);
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(mimeType);
}
return response;
}
public static HttpResponseMessage DownloadAs(this HttpResponseMessage response, string fileName)
{
response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = fileName;
response.Content.Headers.ContentType = MediaTypeHeaderValue.Parse(MimeMapping.GetMimeMapping(fileName));
return response;// For chaining or whatnot
}
My usage in the API controllers is return ResponseMessage(Request.StreamResponse(stream).DownloadAs("Filename.ext"));
I've double checked code for downloading, and this seems to match up with what I've found. What am I doing wrong or what am I missing? It looks like something's wrong with the encoding or charset, but I can't tell what the solution is.
Finally figured out the issue thanks to this Q&A. I was missing the responseType option/parameter in my $http call in the client-side code.
这篇关于C#文件下载已损坏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!