通过HTTP传输文件的Response.TransmitFile的替代方法 [英] Alternative to Response.TransmitFile for transferring files via HTTP

查看:402
本文介绍了通过HTTP传输文件的Response.TransmitFile的替代方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个允许用户下载文件的ASP.NET网站上.

I'm working on a ASP.NET website that allows users to download files.

以前,文件与网站存储在同一服务器上,所以我们可以这样做:

Previously the files were stored on the same server as the website so we could do:

Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
Response.AddHeader("Content-Length", response.ContentLength.ToString());
Response.ContentType = "application/octet-stream";
Response.TransmitFile(path);
Response.End();

但是,现在某些文件存储在单独的服务器上.我可以使用

However, now some of the files are stored on a seperate server. I can verify that the files exist using

WebRequest request = WebRequest.Create(absolute-url);
WebResponse response = request.GetResponse();

但是,由于TransmitFile需要虚拟路径而不是url,我如何方便传输?

But how can I facilitate the transfer as TransmitFile requires a virtual path not a url?

我需要用户能够选择将文件保存为普通网络下载的位置

I need the users to be able to choose where to Save the file as with a normal web download

做到这一点的最佳方法是什么?

What's the best way to do this?

推荐答案

如果可以通过Web请求获取响应流,则应该可以按照以下代码片段将流复制到输出流中:

If you can get the response stream via a web request you should be able to copy the stream to your output stream as per this snippet:

while ((read = stream.Read(buffer, offset, chunkSize)) > 0)    
{

    Response.OutputStream.Write(buffer, 0, read);
    Response.Flush();
}

这篇关于通过HTTP传输文件的Response.TransmitFile的替代方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆