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.