private void Page_Load(object sender, System.EventArgs e)
{
string fPath=Request.QueryString["fonlyName"];
string fileName=fPath;
string filePath=Server.MapPath(fPath);
Response.Clear();
Response.ClearHeaders();
Response.Charset="utf-8";
Response.ContentEncoding=System.Text.Encoding.UTF8;
Response.ContentType="application/octet-stream";
FileInfo fi = new FileInfo(filePath);
Response.AddHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(fileName));
Response.AddHeader("Content-Length",fi.Length.ToString());
byte[] tempbyte = new byte[ 1024*8];
FileStream fs = fi.OpenRead();
int count;
while((count = fs.Read(tempbyte,0,tempbyte.Length))>0)
    {
Response.BinaryWrite(tempbyte);
  Response.Flush();
    }
fs.Close();
Response.End();
}
  要连接这个页面的是文件列举的页,传过来的值是数据库中文件的一个唯一名子,文件是直接放在我的服务器根目录里的,
  这段代码是dl.aspx页面的全部内容.传过来的文件名通过string filePath=Server.MapPath(fPath);得到文件在服务器里的路径.
   执行后没有报错,奇怪的是在也面列举页中点击文件名后有的文件可以下载,有的文件不能下载(此时报错为路径中具有非法字符,string filePath=Server.MapPath(fPath);这句话被加红颜色)
   而且有时是出现windows下载提示,而有时就直接打开了文件出现一些乱码,十分的不稳定.
   请帮忙修改下这个程序或者提供更好点的下载方法.
   就要毕业答辩了,不能让偶去 找些可以下载下来的文件来骗老师们吧!在此先谢过了,请各位帮忙,急急急!

解决方案 »

  1.   

    请试一下, 文件下载:1. C#: 
    /// <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();
    }
    2. vb.net
    Public Sub WriteDLWindow(ByVal strFileName As String, ByVal page As System.Web.UI.Page)
            Try
               If File.Exists(page.MapPath(strFileName)) Then
                   Dim TargetFile As FileInfo = New FileInfo(page.MapPath(strFileName))
                   '清除缓冲区流中的所有内容输出.
                   page.Response.Clear()
                   '向输出流添加HTTP头 [指定下载/保存 对话框的文件名]
                   page.Response.AppendHeader("Content-Disposition", "attachment; filename=" + page.Server.UrlEncode(TargetFile.Name))

    '繁体格式
                    'page.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName, System.Text.Encoding.UTF8))                '向输出流添加HTTP头 [指定文件的长度,这样下载文件就会显示正确的进度
                    page.Response.AppendHeader("Content-Length", TargetFile.Length.ToString())
                    '表明输出的HTTP为流[stream],因此客户端只能下载.
                    page.Response.ContentType = "application/octet-stream"
                    '发送文件流到客户端.
                    page.Response.WriteFile(TargetFile.FullName)
                    '停止执行当前页
                    page.Response.End()
                End If
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
      

  2.   

    同意ChengKing((http://blog.csdn.net/ChengKing)(MSMVP)) 这种情况,你可以使用二进制流的方式对付下载的问题一般来说,开一个新页面,这个页面什么都没有,直接输出二进制流即可,输出完毕就用window.close()关闭窗口就可以了
      

  3.   

    行 14:  if msg_type <> "" then
    行 15:      dim msg_id as integer = request.QueryString("id")
    行 16:  response.writefile("msg.aspx?type=" & msg_type & "&id=" & msg_id)
    行 17:  response.End()
    行 18:  end if
    路径中具有非法字符