【问题标题】:How to download file from HTTPResponseMessage如何从 HTTPResponseMessage 下载文件
【发布时间】:2015-01-26 10:11:43
【问题描述】:

我在我的网站上包含了一个下载图片的链接。当我点击链接时,我希望自动开始下载。

目前,当我单击链接时,我会收到响应消息:示例:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.PushStreamContent, Headers: { Content-Type: application/octet-stream Content-Disposition: attachment;文件名=895621d7-57a4-47a5-8dc5-ae36a2623826Banneraaaaaaaa.jpg }

如何修改以下代码以自动开始下载。我想我可能返回了错误的类型:

这是我的代码:

public HttpResponseMessage DownloadImageFile(string filepath)
        {
            filepath = "https://mysite.com/" + filepath;

            try
            {
                var response = new HttpResponseMessage();
                response.Content = new PushStreamContent((Stream outputStream, HttpContent content, TransportContext context) =>
                {
                    try
                    {
                        DownloadFile(filepath, outputStream);
                    }
                    finally
                    {
                        outputStream.Close();
                    }
                });

                response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/octet-stream");
                response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
                response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(filepath);

                return response;


            }
            catch (Exception ex)
            {

            }

            return null;
        }

        public void DownloadFile(string file, Stream response)
        {
            var bufferSize = 1024 * 1024;

            using (var stream = new FileStream(file, FileMode.Open, FileAccess.Read))
            {
                var buffer = new byte[bufferSize];
                var bytesRead = 0;
                while ((bytesRead = stream.Read(buffer, 0, bufferSize)) > 0)
                {
                    response.Write(buffer, 0, bytesRead);
                }
                response.Flush();
            }
        }

    }

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    您应该使用Controller.File 重载之一。 File() 辅助方法提供对返回文件内容的支持。 MediaTypeNames 类可用于获取特定文件扩展名的 MIME 类型。

    例如:

        public FileResult Download(string fileNameWithPath)
        {
            // Option 1 - Native support for file read
            return File(fileNameWithPath, System.Net.Mime.MediaTypeNames.Application.Octet, Path.GetFileName(fileNameWithPath));
    
            // Option 2 - Read byte array and pass to file object
            //byte[] fileBytes = System.IO.File.ReadAllBytes(fileName); return
            //File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet,
            //fileName);
        }
    

    【讨论】:

      猜你喜欢
      • 2017-04-28
      • 2015-04-09
      • 2015-04-16
      • 1970-01-01
      • 2017-12-17
      • 2011-10-23
      • 1970-01-01
      • 2020-08-22
      • 2014-09-24
      相关资源
      最近更新 更多