我做了一个下载功能,浏览器读不到文件长度,所以没有进度条!
起初以为是没有设置头信息的 Response.AddHeader("Content-Length", fileInfo.Length.ToString());
后来我给了这个给他 也只是本地可以服务器不上下载的话没有效果!
后来我就去抓包看头信息,我发现本地的 是有Content-Length这个头信息的
但是服务器上的没有 取而代之的是多了两个:
Content-Encoding:Gzip 和Transfer-Encoding: chunked
这个Chunked 我查了很多资料知道他的一个功能是分段的等待,
但是没有一个告诉我在asp.net中要怎么设置,或者说怎么取消!
求高手赐教:底下是我的程序代码:
System.IO.FileInfo fileInfo = new System.IO.FileInfo(path);
string downloadfilename = fileInfo.Name;
if (fileInfo.Exists == true)
{
const long ChunkSize = 102400;//100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
byte[] buffer = new byte[ChunkSize];
Response.Clear();
System.IO.FileStream iStream = System.IO.File.OpenRead(path);
long dataLengthToRead = iStream.Length;//获取下载的文件总大小
Response.AddHeader("Content-Length", fileInfo.Length.ToString());
Response.ContentType = "application/octet-stream";
if (HttpContext.Current.Request.ServerVariables["HTTP_USER_AGENT"].IndexOf("MSIE") > -1)
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(downloadfilename.Trim()).Replace("+", " "));
else
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + downloadfilename.Trim());
while (dataLengthToRead > 0 && Response.IsClientConnected)
{
int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//读取的大小
Response.OutputStream.Write(buffer, 0, lengthRead);
Response.Flush();
dataLengthToRead = dataLengthToRead - lengthRead;
}
Response.Close();
}
我也碰到了这个问题,但是我在本地和服务器上跑都是输出Transfer-Encoding: chunked
这样的输出用HTML5 的Audio播放不了
你试试下面地址的第二种方法,有没有用,我这边试了没用,不知道是不是和服务器设置有关
http://www.cnblogs.com/chenjunbiao/archive/2011/05/13/2045783.html
在命令行中运行:
C:\Windows\System32\inetsrv\appcmd set config /section:asp /enableChunkedEncoding:False