【问题标题】:Design for file downloads from a web server从 Web 服务器下载文件的设计
【发布时间】:2009-08-12 16:05:28
【问题描述】:

我的 Web 应用由存储在 SQL Server 数据库中的图像组成。而且,我在客户端有一个 Silverlight 应用程序。 Web 应用程序将允许客户端通过触发 silverlight 应用程序中的下载从服务器下载文件。 Silverlight 与 Web 服务对话以下载文件。

我正在尝试了解 Web 服务端的文件下载逻辑。我可以想出以下方法:

1) 将数据从数据库读取到内存。将内存数据写入服务器上的文件。将服务器路径返回给客户端。客户端会调用带有 url 的 HtmlPage.Window.Navigate 方法来提示用户下载文件。

该方法的缺点:
- 每次下载时都需要将数据库中的数据写入文件。多个同时文件下载请求可能会阻塞 Web 服务器上的硬盘空间。

还有其他下载文件的方法吗? FILESTREAM 的使用是否提供了更好的选择?

感谢您的回复!

【问题讨论】:

    标签: asp.net sql-server web-services filestream


    【解决方案1】:

    由于您已经获得了包含数据库中图像的数据库,因此我将忽略整个“我应该将图像存储在数据库问题中吗”。我在这里只提到它是因为我相信其他人会对此发表评论并为我扣分,因为我没有提到这不是最好的主意。我会尽量回答你的问题。

    您可以让网络服务直接返回图像。这很简单……

    这是一个来自网络服务的代码 sn-p,我只是为了看看你能不能做到这一点。希望您可以根据需要对其进行修改。

    <WebMethod()> _
        Public Function GetImage() As Byte()
            Try
                Dim outStream As New System.IO.MemoryStream
                Dim REturnValue As New System.Drawing.Bitmap(500, 500)
                Dim g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(REturnValue)
                'g.RotateTransform(5)
                Dim f As New System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 16, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point)
                Dim b As System.Drawing.Brush = Drawing.Brushes.Lime
    
                g.DrawString("Hello", f, b, 0, 0)
                g.DrawString("Would you like to play a game? (Y/N)", f, b, 0, 40)
                g.DrawString("> Y", f, b, 0, 80)
                g.DrawString("Loading Global Thermonuclear War,", f, b, 0, 120)
                g.DrawString("please wait...", f, b, 0, 160)
                REturnValue.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    
                Return outStream.ToArray()
            Catch ex As Exception
                Throw New Exception(ex.ToString())
            End Try
    
        End Function
    

    然后是显示图像的 Asp.Net 页面..

     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim ts As New TestServices
            Dim b As System.Drawing.Bitmap
            Dim bytes As Byte()
            Dim inStream As System.IO.MemoryStream
    
            bytes = ts.GetImage()
            inStream = New System.IO.MemoryStream(bytes)
            b = New System.Drawing.Bitmap(inStream)
            Response.ContentType = "image/jpeg"
            b.Save(Response.OutputStream, b.RawFormat)
            b.Dispose()
        End Sub
    

    【讨论】:

    • 呸!不使用/结束使用。 -1 。
    【解决方案2】:

    这是 David Stratton 的回答,刚刚整理完毕:

    <WebMethod()> _
    Public Function GetImage() As Byte()
        Using outStream As New System.IO.MemoryStream
            Using ReturnValue As New System.Drawing.Bitmap(500, 500)
                Using g As System.Drawing.Graphics = System.Drawing.Graphics.FromImage(ReturnValue)
                    'g.RotateTransform(5)
                    Using f As New System.Drawing.Font(System.Drawing.FontFamily.GenericMonospace, 16, Drawing.FontStyle.Regular, Drawing.GraphicsUnit.Point)
                        Dim b As System.Drawing.Brush = Drawing.Brushes.Lime
    
                        g.DrawString("Hello", f, b, 0, 0)
                        g.DrawString("Would you like to play a game? (Y/N)", f, b, 0, 40)
                        g.DrawString("> Y", f, b, 0, 80)
                        g.DrawString("Loading Global Thermonuclear War,", f, b, 0, 120)
                        g.DrawString("please wait...", f, b, 0, 160)
                        ReturnValue.Save(outStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    
                        Return outStream.ToArray()
                    End Using
                End Using
            End Using
        End Using
    End Function
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Using ts As New TestServices
            Dim bytes As Byte() = ts.GetImage()
            Using inStream As System.IO.MemoryStream = New System.IO.MemoryStream(bytes)
                Using b As System.Drawing.Bitmap = New System.Drawing.Bitmap(inStream)
                    Response.ContentType = "image/jpeg"
                    b.Save(Response.OutputStream, b.RawFormat)
                End Using
            End Using
        End Using
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2015-04-04
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-01-07
      • 1970-01-01
      • 2016-03-28
      • 2016-05-29
      • 2021-09-28
      相关资源
      最近更新 更多