如何从HTTPResponseMessage下载文件

时间:2023-02-06 07:56:54

I have included a link on my website to download images. When I click on the link I would like the download to automatically start.

我在我的网站上添加了一个下载图片的链接。当我点击链接时,我希望下载能自动启动。

Currently when I click on the link I’m getting back the response message: Example:

目前,当我点击链接时,我得到了响应消息:示例:

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

状态码:200,推理短语:“OK”,版本:1.1,内容:System.Net.Http。PushStreamContent, header: {Content-Type: application/octet-stream content -处置:attachment;文件名= 895621 d7-57a4-47a5-8dc5-ae36a2623826banneraaaaaaaa.jpg }

How do I modify the code below to start the download automatically. I think I might be returning the wrong type:

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

Here is my code:

这是我的代码:

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();
            }
        }

    }

1 个解决方案

#1


2  

You should use one of the Controller.File overload. The File() helper method provides support for returning the contents of a file. The MediaTypeNames class can be used to get the MIME type for a specific file name extension.

您应该使用其中一个控制器。文件过载。File() helper方法支持返回文件的内容。可以使用MediaTypeNames类获取特定文件名称扩展的MIME类型。

For example:

例如:

    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);
    }

#1


2  

You should use one of the Controller.File overload. The File() helper method provides support for returning the contents of a file. The MediaTypeNames class can be used to get the MIME type for a specific file name extension.

您应该使用其中一个控制器。文件过载。File() helper方法支持返回文件的内容。可以使用MediaTypeNames类获取特定文件名称扩展的MIME类型。

For example:

例如:

    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);
    }