Response.OutputStream.Write Out of Memory 下载太大的文件的时候,内存使用太多

本文介绍了使用ASP.NET实现文件下载的方法,通过逐块读取文件并发送到客户端,适用于大文件下载。同时,提供了使用ASP和ADODB.Stream对象进行流媒体文件传输的示例代码,解决了大文件流媒体播放的问题。

Solution:

http://www.9iyou.com/Program_Data/ASPNET-27564.html

按字节来取
private void Page_Load(object sender, System.EventArgs e)
{
string strDocDir;
string strFile;
strDocDir="updownload";
strFile=Request.QueryString["strFile"];

System.IO.Stream iStream = null;

// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[300000];

// Length of the file:
int length;

// Total bytes to read:
long dataToRead;

// Identify the file to download including its path.
string filepath = Server.MapPath(strDocDir) "\\" strFile;

// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);

// 定义file
System.IO.FileInfo file = new System.IO.FileInfo(filepath);

try
{
// Open the file.
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open,
System.IO.FileAccess.Read,System.IO.FileShare.Read);


// Total bytes to read:
dataToRead = iStream.Length;

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" System.Web.HttpUtility.UrlEncode(file.Name,System.Text.Encoding.UTF8));

// Read the bytes.
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length = iStream.Read(buffer, 0, 300000);

// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length);

// Flush the data to the HTML output.
Response.Flush();

buffer= new Byte[300000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}

}
Response.Clear();
Response.End();
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " ex.Message);
Response.Clear();
Response.End();
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
Response.Clear();
Response.End();
}
}

==============================================================

http://www.longtailvideo.com/support/forum/Setup-Problems/6804/Using-a-Url-for-the-file-parameter

- Turn the buffer on when trying to do chunking of large files:
thus:
response.buffer = true
at the beginning of the script.

- The pack code in PHP is not needed to create the FLV header but can b created by reading the 13 bytes of data at the beginning of the flv file thus:
If (valPos > 0) Then
Response.BinaryWrite objStream.Read(13)
End if

When looping through the file and streaming the 16384 bytes of data ,if the file is sufficiently large you may run out of memory(buffer) and get errors.I fixed this by flushing the response object each time and clearing the buffer so the buffer did not have more than 16384 bytes at any time.Thus:

do while Not objStream.EOS
Response.BinaryWrite objStream.Read (16384)
Response.Flush
Response.Clear
loop


With this I was finally able to stream even my large file ..... good.The code below is th complete stream.asp, you may want to add it to the stream.asp (zip file) code on your http streaming page for people to try it out and hopefully improve on it.


<%
response.Buffer = true
Response.ContentType = "video/x-flv"
//request position and file name
Dim valPos,valFile
valPos = Request("pos")
valFile = Server.Mappath(Request("file"))

//check whether file type is FLV
If (LCase(Right(valFile,Len(valFile)-InstrRev(valFile,"."))) <> "flv")Then
Response.End
End if
//if position was not passed set pos to 0
If (valPos = "") Then
valPos = 0
End if
//create streaming object
Set objStream = Server.CreateObject("ADODB.Stream")
objStream.Open
objStream.Type = 1
objStream.LoadFromFile(valFile)

//if pos is greate than 0 proceed to get FLV header information
If (valPos > 0) Then
Response.BinaryWrite objStream.Read(13)
End if

//set position to begin streaming
objStream.Position = valPos
//loop through file, flush and clear buffer each time
do while Not objStream.EOS
Response.BinaryWrite objStream.Read (163840)
Response.Flush
Response.Clear
loop
//close stream object
objStream.Close
Set objStream = Nothing
%>




The only pain is that this only works for both small and large files on IE, when accessed on Mozilla only the smaller files can be streamed , if you try it on a large file it stops playback.Let me know what your take on that is.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值