首页 >后端开发 >C++ >如何在 ASP.NET 中从远程 URL 下载和流式传输文件?

如何在 ASP.NET 中从远程 URL 下载和流式传输文件?

Mary-Kate Olsen
Mary-Kate Olsen原创
2025-01-06 08:26:43774浏览

How to Download and Stream Files from a Remote URL in ASP.NET?

从 URL 下载/流式传输文件 - asp.net

在某些情况下,可能需要从远程 URL 流式传输或下载文件并触发“另存为”浏览器中的提示。但是,当文件位于虚拟映射目录中时,使用 Server.MapPath 访问其实际位置可能会很困难。

相反,您可以利用 HttpWebRequest 的强大功能来实现此功能。下面是一个允许您传递 Web URL 的示例:

using System.IO;
using System.Net;

namespace FileDownloader
{
    public static class FileDownloader
    {
        public static void DownloadFile(string url, string fileName)
        {
            // Create a stream for the file
            Stream stream = null;

            // Configure streaming chunk size
            int bytesToRead = 10000;

            // Buffer to read bytes in specified chunk size
            byte[] buffer = new Byte[bytesToRead];

            // Initialize response
            HttpWebResponse fileResp = null;

            try
            {
                // Create a WebRequest to get the file
                HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(url);

                // Get a response for this request
                fileResp = (HttpWebResponse)fileReq.GetResponse();

                // Get the Stream returned from the response
                stream = fileResp.GetResponseStream();

                // Prepare the response to the client
                HttpResponse resp = HttpContext.Current.Response;

                // Set response type and add headers
                resp.ContentType = MediaTypeNames.Application.Octet;
                resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
                resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());

                int length;
                do
                {
                    // Verify client connection
                    if (resp.IsClientConnected)
                    {
                        // Read data into the buffer
                        length = stream.Read(buffer, 0, bytesToRead);

                        // Write data to the response's output stream
                        resp.OutputStream.Write(buffer, 0, length);

                        // Flush the data
                        resp.Flush();

                        // Clear the buffer
                        buffer = new Byte[bytesToRead];
                    }
                    else
                    {
                        // Cancel download if client disconnects
                        length = -1;
                    }
                } while (length > 0); // Repeat until no data remains
            }
            finally
            {
                // Close the input stream
                if (stream != null)
                {
                    stream.Close();
                }

                // Dispose the response
                if (fileResp != null)
                {
                    fileResp.Close();
                }
            }
        }
    }
}

通过使用此方法,您可以从远程 URL 流式传输文件,并允许用户使用动态生成的文件名将它们保存在本地。

以上是如何在 ASP.NET 中从远程 URL 下载和流式传输文件?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn