设计从Web服务器下载文件

时间:2009-08-12 16:05:28

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

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

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

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

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

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

感谢您的回复!

2 个答案:

答案 0 :(得分:3)

由于你已经拥有了数据库中带有图像的数据库,因此我将在整个“我应该将图像存储在数据库问题中”。我只是在这里提到它,因为我确信其他人会对它发表评论并给我一些观点,因为它没有提到这不是最好的主意。我会尽我所能回答你的问题。

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

这是我写的一个Web服务的代码片段,看看你是否可以这样做。希望您能根据自己的需要进行修改。

<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