For a new window that opens during downloading and needs to be closed after downloading: it is effective for smaller files:
System.IO.FileInfo file = new System.IO.FileInfo("F:\mp3\mp3\Don't cry my favorite person.mp3");
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename="+Server.UrlEncode("Don't cry my favorite person.mp3"));
Response.AddHeader("Content-Length", file.Length.ToString());
Response.WriteFile(file.FullName);
Response.Flush();
Response.End();
This code can output the file and close the newly opened form.
For large files, it is not better to use the above code, because the above code is output after all files are cached on the server. If your server has heavy traffic and the file is large, it is not recommended to use the above method.
I have tested the following and it seems to work better:
public static bool ResponseFile(HttpRequest _Request,HttpResponse _Response,string _fileName,string _fullPath, long _speed)
{
try
{
FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
try
{
_Response.AddHeader("Accept-Ranges", "bytes");
_Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 10240; //10K bytes
int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (_Request.Headers["Range"] != null)
{
_Response.StatusCode = 206;
string[] range = _Request.Headers["Range"].Split(new char[] {'=', '-'});
startBytes = Convert.ToInt64(range[1]);
}
_Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
_Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength-1, fileLength));
}
_Response.AddHeader("Connection", "Keep-Alive");
_Response.ContentType = "application/octet-stream";
_Response.AddHeader("Content-Disposition","attachment;filename=" + HttpUtility.UrlEncode(_fileName,System.Text.Encoding.UTF8) );
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int) Math.Floor((fileLength - startBytes) / pack) + 1;
for (int i = 0; i < maxCount; i++)
{
if (_Response.IsClientConnected)
{
_Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i=maxCount;
}
}
_Response.Flush();
}
catch
{
return false;
}
finally
{
br.Close();
myFile.Close();
}
}
catch
{
return false;
}
return true;
}
This kind of code is also available on the Internet. It can also output files and close the newly opened window form.
In addition, during the test, it was found that some unexpected things will happen if the operating systems of the server and the client are different. Generally speaking, using If the above code cannot be closed normally, you can check the browser settings in Advanced.
Furthermore, you can modify different output content types Response.ContentType = "application/octet-stream"; because different contenttypes may be interpreted differently on the client side.
If you have any ideas, please reply. Discussions are welcome.
http://www.cnblogs.com/tour-code/archive/2006/10/25/539257.html