我用下面的代码做了下载程序:
public void DownLoadFile_Click(object sender, System.EventArgs e)
{
string fileName=((LinkButton)sender).CommandArgument.ToString().Trim(); if (!File.Exists(fileName))
{
return ;
}
// -- 清除缓冲
HttpContext.Current.Response.Clear(); // 下载文件
Page.Response.AddHeader( "Content-Type", "application/octet-stream" );
Response.Charset = "GB2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode((fileName)));
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000]; // Length of the file:
int length; // Total bytes to read:
long dataToRead; // Identify the file to download including its path.
string filepath = fileName; // Identify the file name.
string filename = System.IO.Path.GetFileName(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;
// 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, 10000); // 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[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
} }
}
}
点击下载按钮后弹出下载对话框,下载完文件后另外一个框架中所有的连接点击后就没反应,而如果在弹出下载对话框后点取消下载则又一切正常,这是怎么回事?
public void DownLoadFile_Click(object sender, System.EventArgs e)
{
string fileName=((LinkButton)sender).CommandArgument.ToString().Trim(); if (!File.Exists(fileName))
{
return ;
}
// -- 清除缓冲
HttpContext.Current.Response.Clear(); // 下载文件
Page.Response.AddHeader( "Content-Type", "application/octet-stream" );
Response.Charset = "GB2312";
HttpContext.Current.Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode((fileName)));
System.IO.Stream iStream = null;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000]; // Length of the file:
int length; // Total bytes to read:
long dataToRead; // Identify the file to download including its path.
string filepath = fileName; // Identify the file name.
string filename = System.IO.Path.GetFileName(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;
// 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, 10000); // 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[10000];
dataToRead = dataToRead - length;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception ex)
{
// Trap the error, if any.
Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
} }
}
}
点击下载按钮后弹出下载对话框,下载完文件后另外一个框架中所有的连接点击后就没反应,而如果在弹出下载对话框后点取消下载则又一切正常,这是怎么回事?
If myNetworkStream.CanWrite Then
myNetworkStream.BeginWrite(myWriteBuffer, 0, myWriteBuffer.Length, New AsyncCallback(AddressOf NetworkStream_ASync_Send_Receive.myWriteCallBack), myNetworkStream)
allDone.WaitOne()
end if
具体 你可以看看msdn
Response.OutputStream.BeginWrite(buffer, 0, length,new AsyncCallback(this.MyCallBack),Response.OutputStream); private void MyCallBack(IAsyncResult ar)
{
NetworkStream str=(NetworkStream)ar.AsyncState;
HttpContext.Current.Response.ClearHeaders();
str.EndWrite(ar); }