asp.net 3.5 (C#) + Vs2008 请教附件下载代码

microlin 2008-10-28 11:21:44
//我现在代码如下写法,但遇到附件是图片,则打开为空,下载下来也是空,但文件大小还是有的.
//如果是文本文件,直接打开中文会成乱码
//希望懂得人看一下代码,哪里有错指正一下,或有现成可用代码提供也行,谢谢

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();
}

}
}
}
...全文
329 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
microlin 2008-10-28
  • 打赏
  • 举报
回复
已经解决了,代码是没问题,我是直接在page页上写的,可能跟某些有关,但原因还是不清楚,现在把这个代码放到.ashx文件上,变OK了,谢谢大家,散分!
7707 2008-10-28
  • 打赏
  • 举报
回复

public void ProcessRequest(HttpContext context)
{

HttpRequest Request = context.Request;
HttpResponse Response = context.Response;

if (string.IsNullOrEmpty(Request.QueryString["ID"]))
return;
int ID = Convert.ToInt32(Request.QueryString["ID"]);
Attachment a = AttachmentClass.GetAttachment(ID);
if (a == null)
{//附件不存在
throw new Exception("Attachment_NotFound");
}

string path = Path.Combine(Sunivo.Components.Configuration.BaseSettings.UpLoadFilePath, a.FilePath);
if (!File.Exists(path))
{
throw new Exception("Attachment_NotFound");
//文件已被删除
}

//检查客户端缓存
if (CachedVersionIsOkay(a.UploadDate.ToUniversalTime(), Request))
{
Response.StatusCode = 304;
Response.SuppressContent = true;
return;
}
Response.ContentType = a.ContentType.ToLower();//MIME类型,和你那段代码相同
Response.Cache.SetLastModified(a.UploadDate);
Response.Cache.SetCacheability(HttpCacheability.Private);

Response.AppendHeader("Content-Disposition", "attachment; filename=\"" + HttpUtility.UrlEncode(a.Filename) + "\"");
Response.TransmitFile(path);

}

private bool CachedVersionIsOkay(DateTime lastWriteTime, HttpRequest Request)
{
string ifModified = Request.Headers["If-Modified-Since"];
if (ifModified != null)
{
string lastModified = lastWriteTime.ToString("r");
return (ifModified == lastModified);
}
return false;
}
7707 2008-10-28
  • 打赏
  • 举报
回复
你的文件存于硬盘?

兄弟,改用这个方法: Response.TransmitFile(path);
microlin 2008-10-28
  • 打赏
  • 举报
回复
你的这个我也用了,结果一样不行.图片依旧是空,文本中文依旧是乱码
cpio 2008-10-28
  • 打赏
  • 举报
回复
/// <summary>
/// 文件下载
/// </summary>
/// <param name="FullFileName"></param>
private void FileDownload(string FullFileName)
{
FileInfo DownloadFile = new FileInfo(FullFileName);
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());
Response.WriteFile(DownloadFile.FullName);
Response.Flush();
Response.End();
}
7707 2008-10-28
  • 打赏
  • 举报
回复
原因是你没清空缓冲区
代码最前面加上 Response.Clear();就行了

110,538

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧