通过我的服务器将文件从远程服务器复制到客户端浏览器,而无需将整个文件写入磁盘或内存 [英] Copy file from remote server to client browser via my server without writing the entire file to disk or memory

查看:162
本文介绍了通过我的服务器将文件从远程服务器复制到客户端浏览器,而无需将整个文件写入磁盘或内存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是场景.

远程文件服务器上有一个文件(例如,我在DropBox上托管了一个文件)

There is a file on a remote file server (say I have a file hosted on DropBox)

我想将该文件作为Web应用程序(c#asp.net 4.0)上的下载文件提供

I want to offer that file as a download on my web application (c# asp.net 4.0)

我想隐藏原始文件100%的位置(我希望它看起来像是来自我的服务器).

I want to hide the location 100% of the original file (I want it to appear to come from my sever).

我不想将此文件写入服务器上的内存或磁盘.

I do not want to write this file to memory or to disk on my server.

我假设我想使用流来流复制.例子

I had assumed that I would want to use a stream to stream copy. example

Stream inputStream = response.GetResponseStream();
inputStream.CopyTo(Response.OutputStream, 4096);

inputStream.Flush();

Response.Flush();
Response.End();

但是,这会将整个流复制到内存中,然后再将其写到客户端浏览器.任何想法都很棒.

This however copies the entire stream into memory before writing it out to the client browser. Any ideas would be awesome.

我需要我的服务器基本上只是充当代理并屏蔽原始文件位置

I need my server to basically just act as a proxy and shield the original file location

感谢您的帮助.

推荐答案

虽然我看不到CopyTo读取内存中整个流的原因(因为任何地方都没有中间流),但是您可以编写相同的副本手动确保其行为符合您想要的方式.

While I don't see reasons why CopyTo will read whole stream in memory (as there is no intermediate stream anywhere), you can write the same Copy manually to be sure it behaves the way you want.

考虑使用异步版本的读/写,以及是否可以使用C#4.0中的async/awit来使代码可读.

Consider using Async versions of read/write and if you can use async/awit from C# 4.0 to make code readable.

旧方法:异步流处理,新方法异步文件I/O

这篇关于通过我的服务器将文件从远程服务器复制到客户端浏览器,而无需将整个文件写入磁盘或内存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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