2011-12-21 28 views
0

我有一個問題WebClient.DownloadFileWebClient.DownloadFile應用

在asp.net服務器我有網頁與此代碼:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load 
    If Not String.IsNullOrEmpty(Me.Request.QueryString("Tool")) Then 
    Dim Subor As String = Server.UrlDecode(Me.Request.QueryString("Tool")) 
    Dim SuborCesta As String = Server.MapPath("~/App_Data/Downloads/Images/" & Subor & ".png") 
    If IO.File.Exists(SuborCesta) Then 
     Response.ClearHeaders() 
     Response.ClearContent() 
     Response.ContentType = "application/octet-stream" 
     Response.AppendHeader("Content-Disposition", "attachment; filename=ToolImage.png") 
     Response.WriteFile(SuborCesta) 
     Response.End() 
    End If 
    End If 
End Sub 

通過這個代碼我下載的圖片IM的桌面程序,我在哪裏使用:

For i As Integer = 0 To ToolList.Count - 1 
    Dim imageAddress As String = String.Empty 
    Dim filename As String = String.Empty 
    Try 
    Dim fileReader As New WebClient() 
    imageAddress = ToolList(i).ToolImage 
    filename = ToolList(i).ToolFullName & ".png" 
    If Not (System.IO.File.Exists(My.Application.Info.DirectoryPath & "\Images\" + filename)) Then 
     fileReader.DownloadFile(New Uri(imageAddress), My.Application.Info.DirectoryPath & "\Images\" + filename) 
    End If 
    Catch ex As Exception 
    MessageBox.Show(ex.Message) 
    End Try 
Next 

如果我在瀏覽器中加載帶查詢字符串的頁面,圖像被下載。但是如果我在我的程序中使用它,我的磁盤上的圖像文件就會創建,但該文件包含我的.aspx頁面中的html代碼。

如何從請求中獲取並保存圖片?

+0

是否包含在您的Web客戶端請求的查詢字符串要求? – N0Alias 2011-12-21 22:36:25

+0

是的,包含查詢字符串。這我首先控制。 – milos 2011-12-21 23:27:20

回答

0

嘗試使用適當的MIME類型:

Response.ContentType = "image/png" 
+0

我試過這個,但它不起作用。它看起來像下載圖像,但方法(WebClient.DownloadFile)只返回沒有圖片的HTML頁面。 – milos 2011-12-21 22:28:53

+0

好吧,如果您還沒有接受答案,我將不得不在明天再次嘗試複製該場景。睡個好覺! – Lloyd 2011-12-22 00:09:53

+0

我仍然在尋找解決方案... – milos 2011-12-22 00:17:43

相關問題