2009-08-12 129 views
1

我的Web應用程序由存儲在SQL Server數據庫中的圖像組成。而且,我在客戶端有一個Silverlight應用程序。 Web應用程序允許客戶端通過在Silverlight應用程序中觸發下載從服務器下載文件。 Silverlight與Web服務通信以下載文件。從Web服務器下載文件的設計

我想了解Web服務端的文件下載邏輯。我可以想出以下方法:

1)從數據讀取數據到內存。將內存數據寫入服務器上的文件。將服務器路徑返回給客戶端。客戶端將調用帶有URL的HtmlPage.Window.Navigate方法來提示用戶下載文件。

方法的缺點:
- 每次下載數據時,數據庫都需要寫入文件。多個同時發生的文件下載請求可能會阻塞Web服務器上的硬盤空間。

有沒有其他的方法來下載文件? FILESTREAM的使用是否提供了更好的選擇?

感謝您的回覆!

回答

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 
+0

呸!沒有使用/結束使用。 -1。 – 2009-08-12 21:30:13

2

這是大衛·斯特拉頓的回答,只是清理:

<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 
+0

看起來好多了。 – David 2009-09-02 04:51:46