数据表中已经有路径和保存的文件名,客户下载的时候想达到重命名的效果,请问如何实现?

解决方案 »

  1.   


        private void FileDownload(string DownPath)
        {
            String FullFileName = Server.MapPath(DownPath);
            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.   

    添加点注释方便以后使用,马上结贴
     private void FileDownload(string DownPath)
        {
            String FullFileName = Server.MapPath(DownPath);
            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));//这里的DownloadFile.FullName为要下载后改成的文件名
            Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
            Response.WriteFile(DownloadFile.FullName);//这里是服务器中实际的文件名
            Response.Flush();
            Response.End();
        }