尝试通过http请求下载视频文件时出现问题 在使用IIS7的以下操作系统中: win2008 32Bit,Win2008 R2 64Bit
目前正常运行:win2003,vista64(IIS6)
问题描述: 当用户通过C#请求大于256mb的文件时,他们会得到一个有限的文件,即使是 使用Content-Length参数,似乎文件大小合适,但不是完整 内容。
当请求文件的URL时,我得到完整的文件,问题只发生在 C#脚本,也是完整缓冲区发送给用户的C#响应。
我在文章中更改了IIS7设置:
http://blog.twinharbor.com/2011/07/28/fixing-iis7-maximum-upload-size/
仍然不起作用。
此外,任何地方都没有备注或错误。
请查看我的代码示例:
var context = System.Web.HttpContext.Current;
context.Response.ContentEncoding = Encoding.GetEncoding("windows-1255");
context.Response.HeaderEncoding = Encoding.GetEncoding("UTF-8");
context.Response.Charset = "utf-8";
System.IO.Stream iStream = null;
// Buffer to read 100K bytes in chunk:
byte[] buffer = new Byte[100000];
// Length of the file:
int length=0;
// Total bytes to read:
long dataToRead=0;
// Identify the file to download including its path.
string filepath = u.Trim(BigFile);
// Identify the file name.
string filename = System.IO.Path.GetFileName(filepath);
Start.Value = u.Time();
try
{
iStream = new System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
dataToRead = iStream.Length;
context.Response.Charset = "";
context.Response.ContentType = "application/octet-stream";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
context.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (context.Response.IsClientConnected)
{
length = iStream.Read(buffer, 0, 100000);
context.Response.OutputStream.Write(buffer, 0, length);
context.Response.Flush();
buffer = new Byte[100000];
dataToRead = dataToRead - length;
}
else
{
dataToRead = -1;
}
}
}
catch (Exception ex)
{
context.Response.Write("Error : " + ex.Message);
}
finally
{
if (iStream != null)
{
iStream.Close();
}
context.Response.Close();
}
我会帮助你。 感谢。