因为IIS支持的最大文件为int32的最大值位数的文件下载,所以,超过2G的文件无法通过IIS进行下载。
通过网上查找的资料,如下可实现文件的下载,使用filestream进行下载。
public void download() { System.IO.Stream iStream = null; byte[] buffer = new Byte[10000];//设置大小 int length; long dataToRead; string filepath = Server.MapPath("~/Upload/XXX/YYY.rar");//文件的路径 string filename = System.IO.Path.GetFileName(filepath); try { iStream = new System.IO.FileStream(filepath, FileMode.Open, FileAccess.Read); dataToRead = iStream.Length; Response.Clear(); Response.ClearHeaders(); Response.ClearContent(); Response.ContentType = "application/unknow"; Response.AddHeader("Content-Length", dataToRead.ToString()); Response.AddHeader("Content-Disposition", "attachment; filename=" + filename); while (dataToRead > 0) { if (Response.IsClientConnected) { length = iStream.Read(buffer, 0, 10000); Response.OutputStream.Write(buffer, 0, length); Response.Flush(); buffer = new Byte[10000]; dataToRead = dataToRead - length; } else { dataToRead = -1; } } } catch (Exception ex) { Response.Write("Error : " + ex.Message); } finally { if (iStream != null) { iStream.Close(); } Response.End(); } }