问题描述
什么是流使用ASP.NET文件的最好方法?
What's the best way to stream files using ASP.NET?
有似乎是这各种方法,目前我使用的。这是用于各种事情,包括发送FLV的从Web根目录以外的嵌入式Flash视频播放器。
There appear to be various methods for this, and I'm currently using the Response.TransmitFile() method inside an http handler, which sends the file to the browser directly. This is used for various things, including sending FLV's from outside the webroot to an embedded Flash video player.
不过,这似乎并不像一个可靠的方法。特别是,有一个奇怪的问题的的Internet Explorer(7)的,在浏览器中的视频后,只是挂或两个被认为。点击任何链接等有没有效果,而且得到的东西在网站上再次合作的唯一方法是关闭浏览器,然后重新打开它。
However, this doesn't seem like a reliable method. In particular, there's a strange problem with Internet Explorer (7), where the browser just hangs after a video or two are viewed. Clicking on any links, etc have no effect, and the only way to get things working again on the site is to close down the browser and re-open it.
这也发生在其他的浏览器,但更频繁。基于一些基本的测试,我怀疑这是值得做的方式文件正在流......也许连接没有被正确关闭,或类似的规定。
This also occurs in other browsers, but much less frequently. Based on some basic testing, I suspect this is something to do with the way files are being streamed... perhaps the connection isn't being closed properly, or something along those lines.
尝试一些不同的东西后,我发现下面的方法对我的作品:
After trying a few different things, I've found that the following method works for me:
Response.WriteFile(path);
Response.Flush();
Response.Close();
Response.End();
这得到周围上面提到的问题,并查看视频不再导致Internet Explorer挂起。
This gets around the problem mentioned above, and viewing videos no longer causes Internet Explorer to hang.
不过,我的理解是,的将文件加载到内存中第一次,并考虑到一些文件被流可能会非常大,这似乎并不像一个理想的解决方案。
However, my understanding is that Response.WriteFile() loads the file into memory first, and given that some files being streamed could potentially be quite large, this doesn't seem like an ideal solution.
我感兴趣的听力如何其他开发人员流大文件在ASP.NET中,特别是,流FLV视频文件。
I'm interested in hearing how other developers are streaming large files in ASP.NET, and in particular, streaming FLV video files.
推荐答案
我想借此的aspx管道以外的事情。特别是,我会写一跑处理器(ASHX,或通过配置映射),但这对的最小的工作,并简单地写入到数据块的响应。处理程序将从查询字符串/表单为正常接受输入,查找流对象,流中的数据(使用一个循环中等大小的本地缓冲区)。下面所示的简单(不完全)例如:
I would take things outside of the "aspx" pipeline. In particular, I would write a ran handler (ashx, or mapped via config), that does the minimum work, and simply writes to the response in chunks. The handler would accept input from the query-string/form as normal, locate the object to stream, and stream the data (using a moderately sized local buffer in a loop). A simple (incomplete) example shown below:
public void ProcessRequest(HttpContext context) {
// read input etx
context.Response.Buffer = false;
context.Response.ContentType = "text/plain";
string path = @"c:\somefile.txt";
FileInfo file = new FileInfo(path);
int len = (int)file.Length, bytes;
context.Response.AppendHeader("content-length", len.ToString());
byte[] buffer = new byte[1024];
Stream outStream = context.Response.OutputStream;
using(Stream stream = File.OpenRead(path)) {
while (len > 0 && (bytes =
stream.Read(buffer, 0, buffer.Length)) > 0)
{
outStream.Write(buffer, 0, bytes);
len -= bytes;
}
}
}
这篇关于最佳流在ASP.NET文件的方式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!