求多浏览器兼容的ASP.NET下载函数
1个回答
展开全部
/// <summary>
/// 下载文件
/// </summary>
private void DownloadFile()
{
string filename = Request.QueryString["path"];
if (string.IsNullOrEmpty(filename))
return;
filename = Path.GetFileName("upload\\" + filename);
if (!Directory.Exists(filename))
return;
int intStart = filename.LastIndexOf("\\") + 1;
filename = filename.Substring(intStart, filename.Length - intStart);
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
Response.ContentType = filecontenttype;
Response.WriteFile(filename);
Response.Flush();
Response.Close();
Response.End();
}
试试
/// 下载文件
/// </summary>
private void DownloadFile()
{
string filename = Request.QueryString["path"];
if (string.IsNullOrEmpty(filename))
return;
filename = Path.GetFileName("upload\\" + filename);
if (!Directory.Exists(filename))
return;
int intStart = filename.LastIndexOf("\\") + 1;
filename = filename.Substring(intStart, filename.Length - intStart);
System.IO.FileInfo fi = new System.IO.FileInfo(filename);
string fileextname = fi.Extension;
string DEFAULT_CONTENT_TYPE = "application/unknown";
RegistryKey regkey, fileextkey;
string filecontenttype;
try
{
regkey = Registry.ClassesRoot;
fileextkey = regkey.OpenSubKey(fileextname);
filecontenttype = fileextkey.GetValue("Content Type", DEFAULT_CONTENT_TYPE).ToString();
}
catch
{
filecontenttype = DEFAULT_CONTENT_TYPE;
}
Response.Clear();
Response.Charset = "utf-8";
Response.Buffer = true;
this.EnableViewState = false;
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + filename);
Response.ContentType = filecontenttype;
Response.WriteFile(filename);
Response.Flush();
Response.Close();
Response.End();
}
试试
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询