2010-07-12 67 views
0

我遇到了從我的SQL數據庫(代碼如下)中讀取圖像數據類型blob的方式問題。當我直接訪問此頁面時,此代碼正常工作。 PDF將打開並工作得很好。但是,當我嘗試使用此頁面下載文件並以編程方式將其保存到文件系統時,它失敗(500服務器錯誤),並且我已驗證它是相同的URL。如何將SQL Server圖像數據類型保存到文件系統?

所以,我的問題是從數據庫映像數據類型下載文件並將其保存到磁盤的最佳方式是什麼?

PDF.aspx

<%@ Page Language="VB" %> 
<%@ Import Namespace="System.Collections.Generic" %> 

<script runat="server"> 
    Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) 
     Page.Theme = String.Empty ' disable theme 
     If (Request.ServerVariables("HTTP_USER_AGENT").IndexOf("MSIE") > 0) Then 
      Response.Cache.SetCacheability(HttpCacheability.Private) 
     Else 
      Response.Cache.SetCacheability(HttpCacheability.NoCache) 
     End If 
    End Sub 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) 
     Dim interns As IEnumerable(Of InternMag.Intern) = InternMag.Intern.GetInternForPhotoOrResume(New Guid(Request.QueryString("studentid"))) 
     For Each intern As InternMag.Intern In interns 
      Response.ContentType = intern.ResumeFileType 
      Response.AddHeader("Content-Disposition", "attachment; filename=""Resume" & intern.ResumeFileExtension & """") 
      Response.BinaryWrite(CType(intern.ResumeFile.ToArray(), Byte())) 
     Next 
    End Sub 
</script> 

回答

0

哇哦 - 這是比我想象的將是比較容易的方式...

System.IO.File.WriteAllBytes(CombinedDocumentPath, intern.ResumeFile.ToArray()) 
2

當您將瀏覽器指向它的網頁作品,但它的錯誤,當你點你的程序了。也許你的WebRequest在HTTP GET請求中省略了所需的studentid。如果您收到錯誤500,此錯誤可能會記錄更多關於的詳細信息,爲什麼在服務器上失敗,因此IIS/ASP錯誤日誌是第一個查找的地方。

至於你發佈的代碼,在內存中讀取整個文件,然後將其封送到Response中是一種處理流的可怕方式。由於每次請求的內存需求增加,它不會擴展並會在負載下終止您的服務器。一個明智的方法是使用CommandBehavior.SequentialAccess來獲得SqlBytes.Stream,然後按chunk將內容寫出chuk。

+0

你知道我在哪裏能得到這個實現的例子嗎? – EdenMachine 2010-07-12 12:20:18

相關問題