if (!this.IsPostBack)
{
try
{
if (SiteCommon.SiteContext.CurrentUser.IsLogon)
{
ds = bid.GetList(" ID = " + Request["id"].ToString() + " ");
if (ds != null)
{
string downFilePath = Server.MapPath("/" + ds.Tables[0].Rows[0]["url"].ToString()); // test with 3.8GB, ok // Server.MapPath("~/files/somefile.iso");
System.IO.FileInfo downFileInfo = new System.IO.FileInfo(downFilePath);
if (!downFileInfo.Exists) throw new Exception("文件不存在。");
const int CHUNK_SIZE = 10000;
byte[] buffer = new byte[CHUNK_SIZE];
Response.Clear();
using (System.IO.FileStream iStream = System.IO.File.OpenRead(downFilePath))
{
long dataLengthToRead = iStream.Length;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition",
"attachment; filename=" + Server.UrlPathEncode(downFileInfo.Name));
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, CHUNK_SIZE);
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
}
Response.Close();
}
}
else
{
Page.RegisterStartupScript("start", "<script>alert('请登录后再进行下载!')</script>");
}
}
catch (System.Exception ex)
{
Page.RegisterStartupScript("start","<script>alert('下载的文件不存在!')</script>");
Console.Write(ex.ToString());
}
}

浙公网安备 33010602011771号