Asp.net web api 为什么Web Api HttpResponseMessage不下载该文件?

Asp.net web api 为什么Web Api HttpResponseMessage不下载该文件?,asp.net-web-api,httpresponse,Asp.net Web Api,Httpresponse,我目前正在对Web Api方法执行GET,并将文件名作为参数传递。当我使用该方法时,我的参数被正确解析,并且文件内容正在写入HttpResponseMessage对象中的响应内容 public HttpResponseMessage Get ([FromUri]string filen) { string downloadPath = WebConfigurationManager.AppSettings["DownloadLocation"];

我目前正在对Web Api方法执行
GET
,并将文件名作为参数传递。当我使用该方法时,我的参数被正确解析,并且文件内容正在写入
HttpResponseMessage
对象中的响应内容

    public HttpResponseMessage Get ([FromUri]string filen)
    {
        string downloadPath = WebConfigurationManager.AppSettings["DownloadLocation"];

        var fileName = string.Format("{0}{1}", downloadPath, filen);
        var response = Request.CreateResponse();

        if (!File.Exists(fileName))
        {
            response.StatusCode = HttpStatusCode.NotFound;
            response.ReasonPhrase = string.Format("The file [{0}] does not exist.", filen);

            throw new HttpResponseException(response);
        }

        response.Content = new PushStreamContent(async (outputStream, httpContent, transportContext) =>
        {
            try
            {
                var buffer = new byte[65536];

                using (var file = File.Open(fileName, FileMode.Open, FileAccess.Read))
                {
                    var length = (int)file.Length;
                    var bytesRead = 1;

                    while (length > 0 && bytesRead > 0)
                    {
                        bytesRead = file.Read(buffer, 0, Math.Min(length, buffer.Length));
                        await outputStream.WriteAsync(buffer, 0, bytesRead);
                        length -= bytesRead;
                    }
                }

            }
            finally
            {
                outputStream.Close();
            }

        });

        response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
        {
            FileName = filen
        };

        return response;
    }
没有看到文件被下载,似乎什么都没有发生。文件好像到处都丢了。我想用浏览器自动下载文件。我确实在fiddler的回复中看到了内容。发生了什么事?任何提示都将不胜感激

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Transfer-Encoding: chunked
Content-Type: application/octet-stream
Expires: -1
Server: Microsoft-IIS/8.0
Content-Disposition: attachment; filename=w-brand.png
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?RDpcSnVubGlcQXN5bmNGaWxlVXBsb2FkV2ViQVBJRGVtb1xBc3luY0ZpbGVVcGxvYWRXZWJBUElEZW1vXGFwaVxGaWxlRG93bmxvYWQ=?=
X-Powered-By: ASP.NET
Date: Thu, 12 Feb 2015 19:32:33 GMT
27b5
PNG
...

我不确定,但您可以尝试使用此代码,它对我有效:

result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = "SampleImg"; 
也许这是一个与你的内容有关的问题

在下一个链接中,您可以看到如何从javascript客户端使用它:


我希望这会有所帮助。

不,PushStreamContent不是问题所在,因为我们可以获得非常好的内容。我试过你的代码,但也没用。我想这可能和我调用它的方式有关吧?你能给我看看你调用它的代码吗?你是怎么调用的?您应该能够使用浏览器调用它。如果它起作用,您就知道问题出在您的客户机代码中@junliantolovichI认为,在另一个问题中,可以帮助解决您的问题:@junliantolovichI如果您添加客户端代码,我将尝试找出它不起作用的原因。window.open(url);这是为客户下载de文档时必须使用的代码@朱利安托洛维奇