GeckoFX Download starten

  • VB.NET
  • .NET (FX) 4.0

Es gibt 3 Antworten in diesem Thema. Der letzte Beitrag () ist von Panics.

    GeckoFX Download starten

    Hallo zusammen

    Vorweg bitte erschlagt mich nicht aber ich habe jetzt Tagelang alles gewälzt was ich im web gefunden habe komme aber nicht weiter...
    Ich nutze in einem Project GeckoFX 31 mit passendem Xulrunner.
    Das ganze funktioniert auch weitestgehend ganz gut bis auf das ich (glaube ich) zu doof bin raus zu bekommen wie ich einen Download mit der GeckoFX initialisiere...
    Downloadmanager und co sind alles kein Problem auch das Navigieren innerhalb des GeckoFX Browsers klappt wunderbar.
    Nur wie kann ich einen Download starten der auf ein ButtonEvent hin startet?

    Leider finde ich dazu kein Tutorial...
    Ich hoffe ihr könnt mir da ein wenig unter die Arme greifen...
    Ich muss nämlich ein Array von ca 400 Dateien darüber downloaden können und komme momentan nicht weiter

    EDIT://
    Entschuldigung ich nutze GeckoFX 33.0.1.0 mit passendem Xulrunner...


    EDIT2://

    VB.NET-Quellcode

    1. Dim P As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\\tmp" ' If Not System.IO.Directory.Exists(P) Then System.IO.Directory.CreateDirectory(P)
    2. Dim objTarget As nsILocalFile = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
    3. Dim tmp_Loc As String = P & "\tmpdload"
    4. Using tmp As New nsAString(tmp_Loc)
    5. objTarget.InitWithPath(tmp)
    6. End Using
    7. If e.FileName.Contains(".") Then
    8. S = Strings.Split(e.FileName, ".")
    9. F = S(S.Length - 1).ToUpper & " File (*." & S(S.Length - 1) & ")|*." & S(S.Length - 1)
    10. Else
    11. F = "File (*.*)|*.*"
    12. End If
    13. savebox.Filter = F '"HTML File (*.html)|*.html"
    14. savebox.Title = "Save File:"
    15. savebox.FileName = e.FileName
    16. If savebox.ShowDialog = System.Windows.Forms.DialogResult.OK And Not String.IsNullOrEmpty(savebox.FileName) Then
    17. Dim source As nsIURI = IOService.CreateNsIUri(New Uri("http://sync.excore-gaming.eu/@excore_gaming/addons/Bornholm.pbo").AbsoluteUri)
    18. Dim dest As nsIURI = IOService.CreateNsIUri(New Uri(savebox.FileName).AbsoluteUri)
    19. Dim t As nsAStringBase = DirectCast(New nsAString(System.IO.Path.GetFileName(savebox.FileName)), nsAStringBase)
    20. Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
    21. Dim DownloadMan As nsIDownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
    22. Dim downloadX As nsIDownload = DownloadMan.AddDownload(0, source, dest, t, e.Mime, 0, Nothing, DirectCast(persist, nsICancelable), False) '
    23. If (downloadX IsNot Nothing) Then
    24. persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
    25. persist.SetProgressListenerAttribute(DirectCast(downloadX, nsIWebProgressListener))
    26. persist.SaveURI(source, Nothing, Nothing, Nothing, Nothing, DirectCast(dest, nsISupports), Nothing)
    27. End If
    28. End If


    Das hab ich gerade gefunden komme damit aber nicht wirklich weiter...
    Ich verstehe nicht so ganz woher ich "e", "F", "S" und "savebox" bekomme.
    Ich denke "savebox" könnte ich als SaveFileDialog einbinden aber der Rest erschließt sich mir gar nicht...

    Ich hoffe ihr könnt mir da ein wenig unter die Arme greifen...

    Gruß
    Panics

    Dieser Beitrag wurde bereits 3 mal editiert, zuletzt von „Panics“ ()

    Danke erst einmal für den Link .
    Leider ist mein c# nicht gerade das beste und ich versteh (wenn überhaupt) nur die hälfte der dort befindlichen Codes...

    Ich habe versucht diese zu Konvertieren aber dabei habe ich wohl mehr Fehler als alles andere gemacht...
    Momentan sieht das ganze so aus:

    VB.NET-Quellcode

    1. Public Shared Function downloadFile(localfile As String, url As String, postData As String) As nsIDownload
    2. Dim DownloadManager As nsIDownloadManager
    3. If DownloadManager Is Nothing Then
    4. DownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
    5. End If
    6. Dim source As nsIURI = IOService.CreateNsIUri(url)
    7. Dim dest As nsIURI = IOService.CreateNsIUri(localfile)
    8. Dim objTarget = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
    9. Using tmp As New nsAString(localfile)
    10. objTarget.InitWithPath(tmp)
    11. End Using
    12. Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
    13. Dim download As nsIDownload = Nothing
    14. Dim t As nsAStringBase = DirectCast(New nsAString(Path.GetFileName(localfile)), nsAStringBase)
    15. download = nsIDownloadManager.AddDownload(0, source, dest, t, Nothing, 0, Nothing, DirectCast(persist, nsICancelable))
    16. If download IsNot Nothing Then
    17. Dim myDown As nsIDownload = download
    18. persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
    19. persist.SetProgressListenerAttribute(DirectCast(myDown, nsIWebProgressListener))
    20. Dim post As nsIInputStream = Nothing
    21. If postData IsNot Nothing AndAlso postData.Length > 5 Then
    22. Dim converter As nsIScriptableUnicodeConverter = Xpcom.CreateInstance(Of nsIScriptableUnicodeConverter)("@mozilla.org/intl/scriptableunicodeconverter")
    23. Dim s As nsAStringBase = DirectCast(New nsAString(postData), nsAStringBase)
    24. converter.SetCharsetAttribute("ASCII")
    25. post = converter.ConvertToInputStream(s)
    26. End If
    27. persist.SaveURI(source, Nothing, Nothing, post, Nothing, DirectCast(dest, nsISupports))
    28. Return myDown
    29. End If
    30. Return Nothing
    31. End Function
    32. Private PostData As String
    33. Private postDataUrl As String
    34. Private Function isChannelMine(channel As nsIHttpChannel) As Boolean
    35. Try
    36. Dim notificationCallbacks As nsIInterfaceRequestor = channel.GetNotificationCallbacksAttribute()
    37. If notificationCallbacks Is Nothing Then
    38. notificationCallbacks = channel.GetLoadGroupAttribute().GetNotificationCallbacksAttribute()
    39. End If
    40. If notificationCallbacks Is Nothing Then
    41. Return False
    42. End If
    43. Dim PtrDomWin As IntPtr = notificationCallbacks.GetInterface(GetType(nsIDOMWindow).GUID)
    44. If PtrDomWin = IntPtr.Zero Then
    45. Return False
    46. End If
    47. Dim domWin As nsIDOMWindow = DirectCast(Marshal.GetObjectForIUnknown(PtrDomWin), nsIDOMWindow)
    48. Return domWin.GetHashCode().Equals(GetHashCode())
    49. Catch
    50. Return False
    51. End Try
    52. End Function
    53. Public Sub Observe(aSubject As nsISupports, aTopic As String, aData As String)
    54. If aTopic.Equals(ObserverNotifications.HttpRequests.HttpOnModifyRequest) Then
    55. Using httpChannel__1 = HttpChannel.Create(aSubject)
    56. If httpChannel__1.Uri IsNot Nothing AndAlso isChannelMine(httpChannel__1._httpChannel) Then
    57. Dim uploadChannel = Xpcom.QueryInterface(Of nsIUploadChannel)(aSubject)
    58. If uploadChannel IsNot Nothing Then
    59. Dim uc = New UploadChannel(uploadChannel)
    60. Dim uploadStream = uc.UploadStream
    61. If uploadStream IsNot Nothing Then
    62. If uploadStream.CanSeek Then
    63. Dim rdr = New BinaryReader(uploadStream)
    64. Dim reqBodyStream = New MemoryStream()
    65. Try
    66. Dim avl As Integer = 0
    67. While (InlineAssignHelper(avl, CInt(uploadStream.Available))) > 0
    68. reqBodyStream.Write(rdr.ReadBytes(avl), 0, avl)
    69. End While
    70. PostData = System.Text.Encoding.ASCII.GetString(reqBodyStream.ToArray())
    71. postDataUrl = httpChannel__1.Uri.AbsoluteUri
    72. reqBodyStream.Dispose()
    73. Catch
    74. End Try
    75. uploadStream.Seek(0, 0)
    76. End If
    77. End If
    78. End If
    79. End If
    80. End Using
    81. End If
    82. End Sub


    Könnte hier mir vielleicht einer sagen wo ich meine Fehler gemacht habe?
    Einige Sachen war ich mir auch absolut nicht sicher es währe also super wenn ihr mir sagen könntet wie ich das besser machen könnte damit ich das umschreiben kann...

    Ich hab nochmal ein wenig gebastelt und nun sieht das ganze so aus:

    VB.NET-Quellcode

    1. Public Shared Function downloadFile(localfile As String, url As String, postData As String) As nsIDownload
    2. Dim DownloadManager As nsIDownloadManager
    3. If DownloadManager Is Nothing Then
    4. DownloadManager = Xpcom.CreateInstance(Of nsIDownloadManager)("@mozilla.org/download-manager;1")
    5. End If
    6. Dim source As nsIURI = IOService.CreateNsIUri(url)
    7. Dim dest As nsIURI = IOService.CreateNsIUri(localfile)
    8. Dim objTarget = Xpcom.CreateInstance(Of nsILocalFile)("@mozilla.org/file/local;1")
    9. Using tmp As New nsAString(localfile)
    10. objTarget.InitWithPath(tmp)
    11. End Using
    12. Dim persist As nsIWebBrowserPersist = Xpcom.CreateInstance(Of nsIWebBrowserPersist)("@mozilla.org/embedding/browser/nsWebBrowserPersist;1")
    13. 'Dim download As nsIDownload = Nothing
    14. Dim t As nsAStringBase = DirectCast(New nsAString(Path.GetFileName(localfile)), nsAStringBase)
    15. Using download As New nsIDownload
    16. download = nsIDownloadManager.AddDownload(0, source, dest, t, Nothing, 0, Nothing, DirectCast(persist, nsICancelable))
    17. If download IsNot Nothing Then
    18. Dim myDown As nsIDownload = download
    19. persist.SetPersistFlagsAttribute(2 Or 32 Or 16384)
    20. persist.SetProgressListenerAttribute(DirectCast(myDown, nsIWebProgressListener))
    21. Dim post As nsIInputStream = Nothing
    22. If postData IsNot Nothing AndAlso postData.Length > 5 Then
    23. Dim converter As nsIScriptableUnicodeConverter = Xpcom.CreateInstance(Of nsIScriptableUnicodeConverter)("@mozilla.org/intl/scriptableunicodeconverter")
    24. Dim s As nsAStringBase = DirectCast(New nsAString(postData), nsAStringBase)
    25. converter.SetCharsetAttribute("ASCII")
    26. post = converter.ConvertToInputStream(s)
    27. End If
    28. persist.SaveURI(source, Nothing, Nothing, post, Nothing, DirectCast(dest, nsISupports))
    29. Return myDown
    30. End If
    31. End Using
    32. Return Nothing
    33. End Function
    34. Private PostData As String
    35. Private postDataUrl As String
    36. Private Function isChannelMine(channel As nsIHttpChannel) As Boolean
    37. Try
    38. Dim notificationCallbacks As nsIInterfaceRequestor = channel.GetNotificationCallbacksAttribute()
    39. If notificationCallbacks Is Nothing Then
    40. notificationCallbacks = channel.GetLoadGroupAttribute().GetNotificationCallbacksAttribute()
    41. End If
    42. If notificationCallbacks Is Nothing Then
    43. Return False
    44. End If
    45. Dim PtrDomWin As IntPtr = notificationCallbacks.GetInterface(GetType(nsIDOMWindow).GUID)
    46. If PtrDomWin = IntPtr.Zero Then
    47. Return False
    48. End If
    49. Dim domWin As nsIDOMWindow = DirectCast(Marshal.GetObjectForIUnknown(PtrDomWin), nsIDOMWindow)
    50. Return domWin.GetHashCode().Equals(GetHashCode())
    51. Catch
    52. Return False
    53. End Try
    54. End Function
    55. Private Shared Function InlineAssignHelper(Of T)(ByRef target As T, ByVal value As T) As T
    56. target = value
    57. Return value
    58. End Function
    59. Public Sub Observe(aSubject As nsISupports, aTopic As String, aData As String)
    60. If aTopic.Equals(ObserverNotifications.HttpRequests.HttpOnModifyRequest) Then
    61. Using httpChannel__1 = HttpChannel.Create(aSubject)
    62. If httpChannel__1.Uri IsNot Nothing AndAlso isChannelMine(httpChannel__1.Uri) Then
    63. Dim uploadChannel = Xpcom.QueryInterface(Of nsIUploadChannel)(aSubject)
    64. If uploadChannel IsNot Nothing Then
    65. Dim uc As New UploadChannel(uploadChannel)
    66. Dim uploadStream = uc.UploadStream
    67. If uploadStream IsNot Nothing Then
    68. If uploadStream.CanSeek Then
    69. Dim rdr = New BinaryReader(uploadStream)
    70. Dim reqBodyStream = New MemoryStream()
    71. Try
    72. Dim avl As Integer = 0
    73. While (InlineAssignHelper(avl, CInt(uploadStream.Available))) > 0
    74. reqBodyStream.Write(rdr.ReadBytes(avl), 0, avl)
    75. End While
    76. PostData = System.Text.Encoding.ASCII.GetString(reqBodyStream.ToArray())
    77. postDataUrl = httpChannel__1.Uri.AbsoluteUri
    78. reqBodyStream.Dispose()
    79. Catch
    80. End Try
    81. uploadStream.Seek(0, 0)
    82. End If
    83. End If
    84. End If
    85. End If
    86. End Using
    87. End If
    88. End Sub


    Jedoch bekomme ich 2 Fehler:
    Fehler 2 "Gecko.Net.UploadChannel.Friend Sub New(uploadChannel As Gecko.nsIUploadChannel)" ist in diesem Kontext nicht zugreifbar, da es "Friend" ist.
    Fehler 1 "New" kann nicht auf einer Schnittstelle verwendet werden. (bezogen auf diese Zeile: "Using download As New nsIDownload")
    Könntet ihr mir dabei helfen?

    Gruß Panics

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Panics“ ()