asp.net实现文件下载(wap手机下载)
2010-01-26 17:18
447 查看
这个问题困扰了我两天,手机下载
protected void Page_Load(object sender, EventArgs e)
{
string filename = Server.UrlDecode(Request["upload"]);
string filePath = Server.MapPath("upload/" + filename);//路径
FileDownload(filePath);
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"> </param>
private void FileDownload(string FullFileName)
{
try
{
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
switch (DownloadFile.Extension.ToLower())//这是必须,电脑上浏览就不需要
{
case ".pdf":
Response.ContentType = "application/pdf";
break;
case ".txt":
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
Response.ContentType = "text/plain";
break;
case ".jpg":
Response.ContentType = "image/jpeg";
break;
case ".doc":
Response.ContentType = "application/msword";
break;
case ".zip":
Response.ContentType = "application/zip";
break;
case ".rar":
Response.ContentType = "application/rar";
break;
case ".xls":
//Response.ContentType = "application/xls";
Response.ContentType = "application/vnd.ms-excel";
break;
default:
Response.ContentType = "application/unknown";
break;
}
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
Label1.Text="文件名不存在";
}
}
catch(Exception ex)
{
Label1.Text = "aa";
Label1.Text = ex.Message;
}
}
protected void Page_Load(object sender, EventArgs e)
{
string filename = Server.UrlDecode(Request["upload"]);
string filePath = Server.MapPath("upload/" + filename);//路径
FileDownload(filePath);
}
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"> </param>
private void FileDownload(string FullFileName)
{
try
{
FileInfo DownloadFile = new FileInfo(FullFileName);
if (DownloadFile.Exists)
{
Response.Clear();
Response.ClearHeaders();
Response.Buffer = false;
//Response.ContentType = "application/octet-stream";//通知浏览器下载文件而不是打开
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.UTF8));
Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
switch (DownloadFile.Extension.ToLower())//这是必须,电脑上浏览就不需要
{
case ".pdf":
Response.ContentType = "application/pdf";
break;
case ".txt":
Response.ClearHeaders();
Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.FullName, System.Text.Encoding.ASCII));
Response.ContentType = "text/plain";
break;
case ".jpg":
Response.ContentType = "image/jpeg";
break;
case ".doc":
Response.ContentType = "application/msword";
break;
case ".zip":
Response.ContentType = "application/zip";
break;
case ".rar":
Response.ContentType = "application/rar";
break;
case ".xls":
//Response.ContentType = "application/xls";
Response.ContentType = "application/vnd.ms-excel";
break;
default:
Response.ContentType = "application/unknown";
break;
}
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
else
{
Label1.Text="文件名不存在";
}
}
catch(Exception ex)
{
Label1.Text = "aa";
Label1.Text = ex.Message;
}
}
相关文章推荐
- 几种asp.net文件下载实现方式
- ASP.Net 下载大文件的实现
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载).NET技术
- ASP.NET利用HttpHandler实现多扩展名文件下载
- ASP.NET实现从服务器下载文件问题处理
- asp.net中实现文件下载功能
- ASP.NET实现文件的上传和下载
- asp.net实现文件下载
- ASP.NET 大文件下载的实现思路及代码
- ASP.NET中实现直接从网页上下载文件
- asp.net实现文件下载功能
- ASP.NET中实现直接从网页上下载文件
- ASP.NET实现文件下载(多种方法,而且都是比较常用的)
- ASP.NET利用HttpHandler实现多扩展名文件下载
- asp.net中用FileStream类实现下载文件功能,自定义下载路径,像IE下载一样
- ASP.NET 实现文件下载的几种方法
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- Asp.net 2.0 用 FileUpload 控件实现多文件上传 用户控件(示例代码下载)
- ASP.net 实现文件强制下载
- 用C#(ASP.NET)怎么实现文件下载功能