我已经编写了一个代码,用于通过Internet Download Manager启用多个连接下载(范围请求),它可以在我的本地系统中正常工作(它可以创建多个连接并下载文件。)但它是无法在我的服务器中创建多个连接(Windows 2012 R2)。
此外,当我保留下载文件并尝试打开我的应用程序的另一个网页时,它会显示加载但未打开(直到我取消下载或让下载完成。)
以下是我的下载代码:
public ActionResult getFile()
{
System.IO.FileStream iStream = null;
var response = System.Web.HttpContext.Current.Response;
var request = System.Web.HttpContext.Current.Request;
// Buffer to read 10K bytes in chunk:
byte[] buffer = new Byte[10000];
int length1;
long dataToRead;
try
{
//File path.
string finalPath = @"C:\myfiles\abc.txt";
long size, start, end, length, fp = 0;
using (StreamReader reader = new StreamReader(finalPath))
{
size = new System.IO.FileInfo(finalPath).Length;
start = 0;
end = size - 1;
length = size;
response.AddHeader("Accept-Ranges", "0-" + size);
if (!String.IsNullOrEmpty(request.Headers["Range"]))
{
long anotherStart = start;
long anotherEnd = end;
string[] arr_split = request.Headers["Range"].Split(new char[] { Convert.ToChar("=") });
string range = arr_split[1];
// Make sure the client hasn't sent us a multibyte range
if (range.IndexOf(",") > -1)
{
// (?) Shoud this be issued here, or should the first
// range be used? Or should the header be ignored and
// we output the whole content?
response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
// If the range starts with an '-' we start from the beginning
// If not, we forward the file pointer
// And make sure to get the end byte if spesified
if (range.StartsWith("-"))
{
// The n-number of the last bytes is requested
anotherStart = size - Convert.ToInt64(range.Substring(1));
}
else
{
arr_split = range.Split(new char[] { Convert.ToChar("-") });
anotherStart = Convert.ToInt64(arr_split[0]);
long temp = 0;
anotherEnd = (arr_split.Length > 1 && Int64.TryParse(arr_split[1].ToString(), out temp)) ? Convert.ToInt64(arr_split[1]) : size;
}
/* Check the range and make sure it's treated according to the specs.
* http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
*/
// End bytes can not be larger than $end.
anotherEnd = (anotherEnd > end) ? end : anotherEnd;
// Validate the requested range and return an error if it's not correct.
if (anotherStart > anotherEnd || anotherStart > size - 1 || anotherEnd >= size)
{
response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
throw new HttpException(416, "Requested Range Not Satisfiable");
}
start = anotherStart;
end = anotherEnd;
fp = reader.BaseStream.Seek(start, SeekOrigin.Begin);
length = end - start + 1; // Calculate new content length
response.StatusCode = 206;
}
}
var fileName1 = Path.GetFileName(finalPath);
var fileNameUrlEncoded = HttpUtility.UrlEncode(fileName1, System.Text.Encoding.UTF8);
// Notify the client the byte range we'll be outputting
response.ContentType = "application/octet-stream";
response.AddHeader("Content-Disposition", "attachment;filename=" +
fileNameUrlEncoded.Replace("+", "%20"));
response.AddHeader("Connection", "Keep-Alive");
response.AddHeader("Content-Range", "bytes " + start + "-" + end + "/" + size);
response.AddHeader("Content-Length", length.ToString());
//response.WriteFile(finalPath, fp, length);
//response.End();
//As WriteFile() method does not work for huge files, thus using
//Chunk downloading below.
string filename = System.IO.Path.GetFileName(finalPath);
iStream = new FileStream(finalPath, System.IO.FileMode.Open,
System.IO.FileAccess.Read, System.IO.FileShare.Read,4096,true);
// Total bytes to read:
dataToRead = length;
iStream.Position = start;
while (dataToRead > 0)
{
// Verify that the client is connected.
if (Response.IsClientConnected)
{
// Read the data in buffer.
length1 = iStream.Read(buffer, 0, 10000);
// Write the data to the current output stream.
Response.OutputStream.Write(buffer, 0, length1);
// Flush the data to the HTML output.
Response.Flush();
buffer = new Byte[10000];
dataToRead = dataToRead - length1;
}
else
{
//prevent infinite loop if user disconnects
dataToRead = -1;
}
}
}
catch (Exception e)
{
TempData["Message"] = "error: " + e.Message;
return View();
}
finally
{
if (iStream != null)
{
//Close the file.
iStream.Close();
}
response.Close();
}
return View();
}
我怀疑导致上述问题的一些服务器配置问题。任何人都可以帮助我知道哪些配置更改可能会导致此问题。
先谢谢。