2012-11-23 140 views
0

我嘗試從服務器下載文件到客戶端時出現問題。點擊保存文件提示符就像它應該顯示的那樣,但它並不指向我想要下載的文件,而是指向我的aspx頁面?換句話說,它不下載我想下載的文件,但它下載了下載鏈接所在的頁面。真的很奇怪......看起來好像我指定的下載文件被完全忽略/沒有效果...錯誤的文件正在下載服務器到客戶端

if (File.Exists(Server.MapPath(driversLocation + name + ".zip"))) 
{ 
    FileInfo fileInfo = new FileInfo(Server.MapPath(driversLocation) + name + ".zip"); 

    Response.Clear(); 
    Response.AddHeader("Content-Disposition", "inline;attachment; filename=" + name + ".zip"); 
    Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
    Response.ContentType = "application/download"; 
    Response.Flush(); 
    Response.TransmitFile(Server.MapPath(driversLocation) + name + ".zip"); 
    Response.End(); 
} 

任何幫助將不勝感激!

回答

0

問題在於「內聯」。還有一些其他的事情來調整,以使代碼更易於閱讀:

相關崗位:Content-Disposition:What are the differences between "inline" and "attachment"?

FileInfo fileInfo = new FileInfo(Server.MapPath(driversLocation + name + ".zip")); 

if (fileInfo.Exists) 
{ 
    Response.Clear(); 
    Response.AddHeader("Content-Disposition", "attachment; filename=" + fileInfo.Name); 
    Response.AddHeader("Content-Length", fileInfo.Length.ToString()); 
    Response.ContentType = "application/x-zip-compressed"; 
    Response.TransmitFile(fileInfo.FullName); 
    Response.End(); 
} 
+0

的感謝!那就是訣竅:D – Alen

相關問題