2013-10-16 54 views
0

我知道還有其他的話題,但沒有任何工作。.7z從我的網站存檔損壞:「無法打開文件作爲存檔。」

在我的網頁上,我允許下載一些配置文件

我使用這個代碼:

public ActionResult DownloadConfigurationFiles() 
{ 
    var stream = this.HttpContext.GetSessionObj<byte[]>((int)WebVariable.ConfigurationFilesStream); 
    if (stream == null) 
    { 
     return HttpNotFound(); 
    } 

    this.HttpContext.ClearSessionObj((int)WebVariable.ConfigurationFilesStream); 
    return File(stream, "application/x-7z-compressed", "ConfigurationFiles.7z"); // octet-stream 
} 

它完美的第一次,當我再試幾秒鐘後,我下載的7z文件,但是當我想打開它,我有這樣的錯誤:

enter image description here

當我踏進代碼,它總是與成功時的第一次嘗試相同......所以我不知道爲什麼我的存檔已損壞..?有什麼方法可以驗證我們構建的檔案是否爲有效的7z文件?

+0

7zip有't'選項來測試文件的完整性。否則,您是否比較了兩個文件的文件大小和/或內容作爲完整性檢查? – blt

回答

1

http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

好像陣列 - 引用類型,您應該複製字節到流例如,才刪除您的會話對象。

public ActionResult DownloadConfigurationFiles() 
{ 
    var bytes = this.HttpContext.GetSessionObj<byte[]>(idx_here); 

    if (bytes != null) // check existance 
    { 
     var target = new MemoryStream(bytes); // <-- don't use USING! 

     this.HttpContext.ClearSessionObj(idx_here); 

     return File(target, mime, file_name); 
    } 

    return HttpNotFound(); 
} 

認爲它有幫助。

相關問題