【问题标题】:How to save a SQL Server image data type to the file system?如何将 SQL Server 图像数据类型保存到文件系统?
【发布时间】:2010-07-12 03:54:34
【问题描述】:

我从 SQL 数据库(下面的代码)读取图像数据类型 blob 的方式存在问题。当我直接访问此页面时,此代码工作正常。 PDF 将打开并且工作正常。但是,当我尝试使用此页面下载文件并以编程方式将其保存到文件系统时,它会失败(500 服务器错误)并且我已经验证它是相同的 URL。

那么,我的问题是从数据库图像数据类型下载文件并将其保存到磁盘的最佳方法是什么?

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>

【问题讨论】:

    标签: sql-server vb.net linq-to-sql file-io


    【解决方案1】:

    当您将浏览器指向该页面时该页面可以工作,但是当您将程序指向该页面时它会出错。也许您的 WebRequest 在 HTTP GET 请求中省略了所需的 studentid。如果您收到错误 500,则此错误可能会记录有关 为什么它在服务器上失败的更多详细信息,因此首先要查看 IIS/ASP 错误日志。

    至于您发布的代码,读取内存中的整个文件然后将其编组到响应中是一种处理流的可怕方式。由于每个请求的内存需求增加,它不会扩展并且会在负载下杀死您的服务器。一种明智的方法是使用CommandBehavior.SequentialAccess 获取SqlBytes.Stream,然后将内容逐块写入。

    【讨论】:

    • 你知道我在哪里可以得到这个实现的例子吗?
    【解决方案2】:

    哇哦 - 这比我想象的要容易得多......

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

    【讨论】:

      猜你喜欢
      • 2013-03-07
      • 1970-01-01
      • 1970-01-01
      • 2012-08-10
      • 1970-01-01
      • 1970-01-01
      • 2012-06-12
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多