2013-01-01 49 views
0

我正在使用下面的代码从服务器下载文件到客户端机器,但是当文件保存它时,会保存完整路径名称,然后是扩展名(例如:Images/24/12/green.png),但我要存储文件只在客户端machine.how名(green.png)能不能做到asp.net文件下载问题

string imagePath = String.Format("~/Images/{0}/{1}", item.Value,item.Text); 
       try 
       { 

        System.Net.WebClient req = new System.Net.WebClient(); 
        HttpResponse response = HttpContext.Current.Response; 
        response.Clear(); 
        response.ClearContent(); 
        response.ClearHeaders(); 
        response.Buffer = true; 
        response.AddHeader("Content-Disposition","attachment;filename=\""+ imagePath + "\""); 
        //byte[] data = req.DownloadData(imagePath); 
        //response.BinaryWrite(data); 
        response.TransmitFile(imagePath); 
        response.End(); 
       } 
       catch(Exception ex) 
       { 

       } 

回答

4

只有在Content-Disposition头使用的图像名称,而不是整个路径:

string fileName = Path.GetFileName(imagePath); 
response.AddHeader("Content-Disposition", 
        "attachment;filename=\""+ fileName + "\""); 

您已将文件名称作为文件以及服务器端路径。

+0

感谢您分享丢失的信息。 – sai