FineUI 官方论坛
标题:
appbpx框架下下载文件的代码不起作用
[打印本页]
作者:
vrwqq
时间:
2012-10-1 11:15
标题:
appbpx框架下下载文件的代码不起作用
如题,哪位能指点一下,谢谢!具体代码如下:
public static bool DownloadFile( string sUrl, string clientFileName)
{
string filePath = HttpContext.Current.Server.MapPath(sUrl);//路径
FileInfo fileInfo = new FileInfo(filePath);
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader binReader = new BinaryReader(fileStream);
try
{
if (fileInfo.Exists)
{
long startBytes = 0;
string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(filePath).ToString("r");
string _EncodedData = HttpUtility.UrlEncode(clientFileName, Encoding.UTF8) + lastUpdateTiemStamp;
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = false;
HttpContext.Current.Response.AddHeader("Accept-Ranges", "bytes");
HttpContext.Current.Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
HttpContext.Current.Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);
HttpContext.Current.Response.AddHeader("Content-Length", (fileInfo.Length - startBytes).ToString());
HttpContext.Current.Response.AddHeader("Connection", "Keep-Alive");
HttpContext.Current.Response.ContentEncoding = Encoding.UTF8;
//Send data
binReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
//Dividing the data in 1024 bytes package
int maxCount = (int)Math.Ceiling((fileInfo.Length - startBytes + 0.0) / 1024);
//Download in block of 1024 bytes
int i;
for (i = 0; i < maxCount && HttpContext.Current.Response.IsClientConnected; i++)
{
HttpContext.Current.Response.BinaryWrite(binReader.ReadBytes(1024));
HttpContext.Current.Response.Flush();
}
//if blocks transfered not equals total number of blocks
if (i < maxCount)
return false;
return true;
}
else
return false;
}
catch (Exception ex)
{
string s = ex.Message;
return false;
}
finally
{
HttpContext.Current.Response.End();
binReader.Close();
fileStream.Close();
}
}
作者:
CoolHots
时间:
2012-10-4 05:50
在IIS调试和在VS直接调试,下载的路径是不一样的
欢迎光临 FineUI 官方论坛 (https://fineui.com/bbs/)
Powered by Discuz! X3.4