还是用切分法了. public static void DownLoadtoClient(string _path,System.Web.UI.Page _page) { 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 = _path; try { if(File.Exists(_path)) { // 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; _page.Response.ContentType = "application/octet-stream"; _page.Response.AddHeader("Content-Disposition", "attachment; filename=" + CExcelManager.GetFileName()); _page.Response.AppendHeader("Content-Length",dataToRead.ToString()); // Read the bytes. while (dataToRead > 0) { // Verify that the client is connected. if (_page.Response.IsClientConnected) { // Read the data in buffer. length = iStream.Read(buffer, 0, 10000); // Write the data to the current output stream. _page.Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output. _page.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. _page.Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { //Close the file. iStream.Close(); } }
当我到2003下载时就不行了,是不是有限制大小,或者象上传文件一样可以对WebConfig进行配置呢?:我又试了在2003上把文件改小点发现就可以下载!请高手指教!
public static void DownLoadtoClient(string _path,System.Web.UI.Page _page)
{ 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 = _path;
try
{
if(File.Exists(_path)) {
// 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; _page.Response.ContentType = "application/octet-stream";
_page.Response.AddHeader("Content-Disposition", "attachment; filename=" + CExcelManager.GetFileName());
_page.Response.AppendHeader("Content-Length",dataToRead.ToString());
// Read the bytes.
while (dataToRead > 0) {
// Verify that the client is connected.
if (_page.Response.IsClientConnected) {
// Read the data in buffer.
length = iStream.Read(buffer, 0, 10000); // Write the data to the current output stream.
_page.Response.OutputStream.Write(buffer, 0, length); // Flush the data to the HTML output.
_page.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.
_page.Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
}