當前位置:編程學習大全網 - 遊戲軟體 - 壹個關於asp.net(c#)文件的問題

壹個關於asp.net(c#)文件的問題

文件路徑是不是有中文啊?zh-tw 這是繁體的!

asp.net2.0以後,我們有了很多種文件下載的選擇。

public partial class FileDownLoad : System.Web.UI.Page

{

//提供下載的文件,不編碼的話文件名會亂碼

private string fileName = HttpContext.Current.Server.UrlEncode("規範.rar");

private string filePath = HttpContext.Current.Server.MapPath("規範.rar");

//使用TransmifFile下載文件

protected void btnDL1_Click(object sender, EventArgs e)

{

FileInfo info = new FileInfo(filePath);

long fileSize = info.Length;

Response.Clear();

Response.ContentType = "application/x-zip-compressed";

Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName);

//不指明Content-Length用Flush的話不會顯示下載進度

Response.AddHeader("Content-Length", fileSize.ToString());

Response.TransmitFile(filePath, 0, fileSize);

Response.Flush();

Response.Close();

}

//使用WriteFile下載文件

protected void btnDL2_Click(object sender, EventArgs e)

{

FileInfo info = new FileInfo(filePath);

long fileSize = info.Length;

Response.Clear();

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);

//指定文件大小

Response.AddHeader("Content-Length", fileSize.ToString());

Response.WriteFile(filePath, 0, fileSize);

Response.Flush();

Response.Close();

}

//使用OutputStream.Write分塊下載文件

protected void btnDL3_Click(object sender, EventArgs e)

{

//指定塊大小

long chunkSize = 102400;

//建立壹個100K的緩沖區

byte[] buffer = new byte[chunkSize];

//已讀的字節數

long dataToRead = 0;

FileStream stream = null;

try

{

//打開文件

stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

dataToRead = stream.Length;

//添加Http頭

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);

Response.AddHeader("Content-Length", dataToRead.ToString());

while (dataToRead > 0)

{

if (Response.IsClientConnected)

{

int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));

Response.OutputStream.Write(buffer, 0, length);

Response.Flush();

Response.Clear();

dataToRead -= length;

}

else

{

//防止client失去連接

dataToRead = -1;

}

}

}

catch (Exception ex)

{

Response.Write("Error:" + ex.Message);

}

finally

{

if (stream != null)

{

stream.Close();

}

Response.Close();

}

}

//使用BinaryWrite下載文件,大文件效率不行

protected void btnDL4_Click(object sender, EventArgs e)

{

FileStream stream = null;

try

{

//讀文件,大文件壹次讀入會占用大量內存

stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

byte[] bytes = new byte[stream.Length];

stream.Read(bytes, 0, bytes.Length);

stream.Close();

//添加Http頭

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);

Response.AddHeader("Content-Length", bytes.Length.ToString());

Response.BinaryWrite(bytes);

Response.Flush();

}

catch (Exception ex)

{

Response.Write("Error:" + ex.Message);

}

finally

{

if (stream != null)

{

stream.Close();

}

Response.Close();

}

}

//使用BinaryWrite分塊下載文件

protected void btnDL5_Click(object sender, EventArgs e)

{

//指定區塊和緩沖區

long chunkSize = 102400;

byte[] buffer = new byte[chunkSize];

FileStream stream = null;

long dataToRead = 0;

try

{

stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);

dataToRead = stream.Length;

//添加Http頭

Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName);

Response.AddHeader("Content-Length", dataToRead.ToString());

while (dataToRead > 0)

{

if (Response.IsClientConnected)

{

int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));

Response.BinaryWrite(buffer);

Response.Flush();

Response.Clear();

dataToRead -= length;

}

else

{

dataToRead = -1;

}

}

}

catch(Exception ex)

{

Response.Write("Error:" + ex.Message);

}

finally

{

if (stream != null)

{

stream.Close();

}

Response.Close();

}

}

}

public partial class FileDownLoad : System.Web.UI.Page { //提供下載的文件,不編碼的話文件名會亂碼 private string fileName = HttpContext.Current.Server.UrlEncode("規範.rar"); private string filePath = HttpContext.Current.Server.MapPath("規範.rar"); //使用TransmifFile下載文件 protected void btnDL1_Click(object sender, EventArgs e) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/x-zip-compressed"; Response.AddHeader("Content-Disposition", "attachment;filename="+ fileName); //不指明Content-Length用Flush的話不會顯示下載進度 Response.AddHeader("Content-Length", fileSize.ToString()); Response.TransmitFile(filePath, 0, fileSize); Response.Flush(); Response.Close(); } //使用WriteFile下載文件 protected void btnDL2_Click(object sender, EventArgs e) { FileInfo info = new FileInfo(filePath); long fileSize = info.Length; Response.Clear(); Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); //指定文件大小 Response.AddHeader("Content-Length", fileSize.ToString()); Response.WriteFile(filePath, 0, fileSize); Response.Flush(); Response.Close(); } //使用OutputStream.Write分塊下載文件 protected void btnDL3_Click(object sender, EventArgs e) { //指定塊大小 long chunkSize = 102400; //建立壹個100K的緩沖區 byte[] buffer = new byte[chunkSize]; //已讀的字節數 long dataToRead = 0; FileStream stream = null; try { //打開文件 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http頭 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); Response.Clear(); dataToRead -= length; } else { //防止client失去連接 dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } //使用BinaryWrite下載文件,大文件效率不行 protected void btnDL4_Click(object sender, EventArgs e) { FileStream stream = null; try { //讀文件,大文件壹次讀入會占用大量內存 stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); byte[] bytes = new byte[stream.Length]; stream.Read(bytes, 0, bytes.Length); stream.Close(); //添加Http頭 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", bytes.Length.ToString()); Response.BinaryWrite(bytes); Response.Flush(); } catch (Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } //使用BinaryWrite分塊下載文件 protected void btnDL5_Click(object sender, EventArgs e) { //指定區塊和緩沖區 long chunkSize = 102400; byte[] buffer = new byte[chunkSize]; FileStream stream = null; long dataToRead = 0; try { stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read); dataToRead = stream.Length; //添加Http頭 Response.ContentType = "application/octet-stream"; Response.AddHeader("Content-Disposition", "attachement;filename=" + fileName); Response.AddHeader("Content-Length", dataToRead.ToString()); while (dataToRead > 0) { if (Response.IsClientConnected) { int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize)); Response.BinaryWrite(buffer); Response.Flush(); Response.Clear(); dataToRead -= length; } else { dataToRead = -1; } } } catch(Exception ex) { Response.Write("Error:" + ex.Message); } finally { if (stream != null) { stream.Close(); } Response.Close(); } } }

以上除了第四種不推薦以外,其他的都可以,但是個人感覺分塊下載的要好壹點。沒有仔細測試,所以可能有問題。

註意:對於中文文件名要編碼才能正確顯示。對於長中文文件名(UTF8編碼後大於153字節的中文)即使編碼了,還是有問題的,大家可以參考這個鏈接。

/river_5566/blog/item/d66804cef8afb031b700c863.html

  • 上一篇:python封裝成exe如何加密
  • 下一篇:這是哪部動畫片的?
  • copyright 2024編程學習大全網