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

lnlaulya  于 2023-01-10  发布在  .NET
关注(0)|答案(8)|浏览(189)

我需要流文件,这将导致保存为提示在浏览器中。问题是,该文件所在的目录是虚拟Map,所以我无法使用服务器。MapPath来确定它的实际位置。该目录是不是在同一个位置(或甚至物理服务器上的活框)作为网站。
我想像下面这样,但这将允许我传递一个Web URL,而不是一个服务器文件路径。
我可能不得不从一个配置基路径构建我的文件路径,然后在路径的其余部分追加,但希望我可以这样做。

var filePath = Server.MapPath(DOCUMENT_PATH);

if (!File.Exists(filePath))
    return;

var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();
bejyjqdl

bejyjqdl1#

您可以使用HttpWebRequest来获取文件并将其流回客户机,这允许您获取带有url的文件,我找到了一个这样的示例(但不记得在哪里给予)

//Create a stream for the file
    Stream stream = null;

    //This controls how many bytes to read at a time and send to the client
    int bytesToRead = 10000;

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

    // The number of bytes read
    try
    {
      //Create a WebRequest to get the file
      HttpWebRequest fileReq = (HttpWebRequest) HttpWebRequest.Create(url);

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

      if (fileReq.ContentLength > 0)
        fileResp.ContentLength = fileReq.ContentLength;

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

        // prepare the response to the client. resp is the client Response
        var resp = HttpContext.Current.Response;

        //Indicate the type of data being sent
        resp.ContentType = MediaTypeNames.Application.Octet;
    
        //Name the file 
        resp.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
        resp.AddHeader("Content-Length", fileResp.ContentLength.ToString());
    
        int length;
        do
        {
            // Verify that the client is connected.
            if (resp.IsClientConnected)
            {
                // Read data into the buffer.
                length = stream.Read(buffer, 0, bytesToRead);

                // and write it out 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 the download if client has disconnected
                length = -1;
            }
        } while (length > 0); //Repeat until no data is read
    }
    finally
    {
        if (stream != null)
        {
            //Close the input stream
            stream.Close();
        }
    }
aydmsdu9

aydmsdu92#

下载url到字节并将字节转换为流:

using (var client = new WebClient())
{
    var content = client.DownloadData(url);
    using (var stream = new MemoryStream(content))
    {
        ...
    }
}
x6h2sr28

x6h2sr283#

我经常这样做,我想我可以添加一个更简单的答案。我在这里将它设置为一个简单的类,但我每天晚上都运行它来收集我跟踪的公司的财务数据。

class WebPage
{
    public static string Get(string uri)
    {
        string results = "N/A";

        try
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uri);
            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            StreamReader sr = new StreamReader(resp.GetResponseStream());
            results = sr.ReadToEnd();
            sr.Close();
        }
        catch (Exception ex)
        {
            results = ex.Message;
        }
        return results;
    }
}

在本例中,我传入一个url,它返回HTML格式的页面,如果你想对流做一些不同的事情,你可以很容易地改变它。
您可以这样使用它:

string page = WebPage.Get("http://finance.yahoo.com/q?s=yhoo");
gab6jxml

gab6jxml4#

2年后,我使用了达拉斯的答案,但我必须将HttpWebRequest更改为FileWebRequest,因为我链接到直接文件。不确定是否所有地方都是这样,但我想我会添加它。此外,我删除了
var resp = Http.Current.Resonse
并且只是在引用resp的地方使用Http.Current.Response

lvmkulzt

lvmkulzt5#

如果您正在寻找@达拉斯答案的.NET核心版本,请使用下面的。

Stream stream = null;

        //This controls how many bytes to read at a time and send to the client
        int bytesToRead = 10000;

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

        // The number of bytes read
        try
        {
            //Create a WebRequest to get the file
            HttpWebRequest fileReq = (HttpWebRequest)HttpWebRequest.Create(@"file url");

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

            if (fileReq.ContentLength > 0)
                fileResp.ContentLength = fileReq.ContentLength;

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

            // prepare the response to the client. resp is the client Response
            var resp = HttpContext.Response;

            //Indicate the type of data being sent
            resp.ContentType = "application/octet-stream";

            //Name the file 
            resp.Headers.Add("Content-Disposition", "attachment; filename=test.zip");
            resp.Headers.Add("Content-Length", fileResp.ContentLength.ToString());

            int length;
            do
            {
                // Verify that the client is connected.
                if (!HttpContext.RequestAborted.IsCancellationRequested)
                {
                    // Read data into the buffer.
                    length = stream.Read(buffer, 0, bytesToRead);

                    // and write it out to the response's output stream
                    resp.Body.Write(buffer, 0, length);

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

xsuvu9jc6#

我认为在.Net Core中最简单的方法是:

using (MemoryStream ms = new MemoryStream())
    using (HttpClient client = new HttpClient())
    {
      client.GetStreamAsync(url).Result.CopyTo(ms);
      // use ms in what you want
    }
  }

现在你有这个文件下载作为流里面ms.

o3imoua4

o3imoua47#

您可以尝试使用带有IIS路径前缀的DirectoryEntry类:

using(DirectoryEntry de = new DirectoryEntry("IIS://Localhost/w3svc/1/root" + DOCUMENT_PATH))
{
    filePath = de.Properties["Path"].Value;
}

if (!File.Exists(filePath))
        return;

var fileInfo = new System.IO.FileInfo(filePath);
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", String.Format("attachment;filename=\"{0}\"", filePath));
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.WriteFile(filePath);
Response.End();
eaf3rand

eaf3rand8#

如果我们在Citrix Netscaler上使用负载均衡器(没有WAF策略),Dallas提供的公认解决方案对我们有效。
当文件与WAF关联时,文件的下载无法通过Netscaler的LB进行,因为当前场景(内容长度不正确)违反RFC,AppFW重置连接,当WAF策略未关联时,不会发生这种情况。
所以缺少的是:

    • 响应.结束();**

另请参阅:Trying to stream a PDF file with asp.net is producing a "damaged file"

相关问题