C#文件下载已损坏 [英] C# File Download is Corrupt

查看:193
本文介绍了C#文件下载已损坏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个Web API项目的实用程序中有一些C#。代码的上传部分工作正常;我已经验证了获取到服务器的文件与上传的文件相匹配。但是,下载中发生的一些事情导致客户端将文件查看为损坏,而当我做差异时,我可以看到出现问题。





不幸的是,我不知道我做错了什么。实用程序的相关部分如下:

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆