asp.net 3.5 (C#) + Vs2008 请教附件下载代码
//我现在代码如下写法,但遇到附件是图片,则打开为空,下载下来也是空,但文件大小还是有的.
//如果是文本文件,直接打开中文会成乱码
//希望懂得人看一下代码,哪里有错指正一下,或有现成可用代码提供也行,谢谢
public partial class FileDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string oldName = Server.UrlDecode(Request.QueryString["OldName"]);
string newName = Server.UrlDecode(Request.QueryString["NewName"]);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
string url = Path.Combine(ConfigInfo.AnnexsMapPath, newName);
if (System.IO.File.Exists(url))
{
FileInfo info = new FileInfo(url);
string flName = info.Extension;
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[(int)(fs.Length)];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
string ContentType = "";
switch (flName.ToLower())
{
case ".asf":
ContentType = "video/x-ms-asf";
break;
case ".avi":
ContentType = "video/avi";
break;
case ".doc":
ContentType = "application/msword";
break;
case ".zip":
ContentType = "application/zip";
break;
case ".xls":
ContentType = "application/vnd.ms-excel";
break;
case ".gif":
ContentType = "image/gif";
break;
case ".jpg":
case "jpeg":
ContentType = "image/jpeg";
break;
case ".wav":
ContentType = "audio/wav";
break;
case ".mp3":
ContentType = "audio/mpeg3";
break;
case ".mpg":
case "mpeg":
ContentType = "video/mpeg";
break;
case ".rtf":
ContentType = "application/rtf";
break;
case ".htm":
case "html":
ContentType = "text/html";
break;
case ".txt":
ContentType = "text/plain";
break;
default:
ContentType = "application/octet-stream";
break;
}
Response.AddHeader("Content-Disposition", ("attachment; filename=" + HttpUtility.UrlEncode(oldName, System.Text.Encoding.UTF8)));
Response.AddHeader("Content-Length", buffer.Length.ToString());
// Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = ContentType;
Response.BinaryWrite(buffer);
Response.Flush();
}
}
}
}
//如果是文本文件,直接打开中文会成乱码
//希望懂得人看一下代码,哪里有错指正一下,或有现成可用代码提供也行,谢谢
public partial class FileDown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string oldName = Server.UrlDecode(Request.QueryString["OldName"]);
string newName = Server.UrlDecode(Request.QueryString["NewName"]);
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Clear();
string url = Path.Combine(ConfigInfo.AnnexsMapPath, newName);
if (System.IO.File.Exists(url))
{
FileInfo info = new FileInfo(url);
string flName = info.Extension;
FileStream fs = new FileStream(url, FileMode.Open, FileAccess.Read);
byte[] buffer = new byte[(int)(fs.Length)];
fs.Read(buffer, 0, buffer.Length);
fs.Close();
string ContentType = "";
switch (flName.ToLower())
{
case ".asf":
ContentType = "video/x-ms-asf";
break;
case ".avi":
ContentType = "video/avi";
break;
case ".doc":
ContentType = "application/msword";
break;
case ".zip":
ContentType = "application/zip";
break;
case ".xls":
ContentType = "application/vnd.ms-excel";
break;
case ".gif":
ContentType = "image/gif";
break;
case ".jpg":
case "jpeg":
ContentType = "image/jpeg";
break;
case ".wav":
ContentType = "audio/wav";
break;
case ".mp3":
ContentType = "audio/mpeg3";
break;
case ".mpg":
case "mpeg":
ContentType = "video/mpeg";
break;
case ".rtf":
ContentType = "application/rtf";
break;
case ".htm":
case "html":
ContentType = "text/html";
break;
case ".txt":
ContentType = "text/plain";
break;
default:
ContentType = "application/octet-stream";
break;
}
Response.AddHeader("Content-Disposition", ("attachment; filename=" + HttpUtility.UrlEncode(oldName, System.Text.Encoding.UTF8)));
Response.AddHeader("Content-Length", buffer.Length.ToString());
// Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.UTF8;
Response.ContentType = ContentType;
Response.BinaryWrite(buffer);
Response.Flush();
}
}
}
}
...全文
相关推荐
asp.net文件处理
asp.net 文件上传下载代码,vs2008
VS2010 ASP.NET C# WEB开发(源代码)
ASP.NET C# 开发的门户网站的源代码,实测VS2010打开没问题,使用时用VS2010新建一个网站项目,将所有文件拷入项目文件夹,再用VS2010打开项目即可。
asp.net课程设计源码vs2010 sqlsever2008
asp.net开源项目,代码全,内有数据库生成文件导入可用
vS2010 ASP.NET C# WEB开发(源代码)
ASP.NET C# 开发的门户网站的源代码,实测VS2010打开没问题,使用时用VS2010新建一个网站项目,将所有文件拷入项目文件夹,再用VS2010打开项目即可
asp.net+sqlserver2005旅游管理系统(vs2008平台开发的C#版)
大三时候asp.net期末作业,功能基本实现,只是数据没怎么填充,后台管理员admin,密码00 。 本系统采用C#+sqlserver2005在vs2008平台下开发,全部都是自己手写代码,比较合适学生学习之用,是初学者的不错选择。(实现旅游信息的发布,修改,删除等,还有旅游路线的等,有旅馆住宿信息发布。特色:采用ckeditor和ckfinder作为信息发布编辑器,实现所见即所得功能,还自动生成静态信息页面,减轻服务器负担。)


