替代Response.TransmitFile通过HTTP传送文件 [英] Alternative to Response.TransmitFile for transfering files via HTTP

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

问题描述

我工作的一个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?

我需要的用户能够选择在哪里将文件保存为一个正常的Web下载

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

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

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