Moin zusammen,
ich habe ein Programm geschrieben, welches Dateien auf einen WEBDav-Server hoch- und runterladt.
Dazu habe ich die folgenden Funktionen/Beispiele gefunden:
WEBDav Upload
WebDav Download
Funktioniert auf meinem PC auch sehr gut. Allerdings funktioniert es nicht mehr ab Framework 4.0
Und ich komme nicht dahinter warum... Das muss irgendwas mit zutun haben.
Wenn ich es richtig verstanden habe, gibt es das im Framework 4 nicht mehr.
Kann mir einer helfen, die Funktion für das 4er abzuändern?
Hier nochmal die Funktionen:
Spoiler anzeigen
Würde mich über eure Hilfe freuen
ich habe ein Programm geschrieben, welches Dateien auf einen WEBDav-Server hoch- und runterladt.
Dazu habe ich die folgenden Funktionen/Beispiele gefunden:
WEBDav Upload
WebDav Download
Funktioniert auf meinem PC auch sehr gut. Allerdings funktioniert es nicht mehr ab Framework 4.0
Und ich komme nicht dahinter warum... Das muss irgendwas mit zutun haben.
Wenn ich es richtig verstanden habe, gibt es das im Framework 4 nicht mehr.
Kann mir einer helfen, die Funktion für das 4er abzuändern?
Hier nochmal die Funktionen:
VB.NET-Quellcode
- Public Function XML_hochladen(ByVal Dateipfad As String, ByVal Serverpfad As String, ByVal ConPort As String, ByVal Benutzername As String, ByVal Passwort As String) As Boolean
- 'C: The first thing we can do is get the path of the file, and it's length.
- Dim fileToUpload As String = Dateipfad
- Dim fileLength As Long = My.Computer.FileSystem.GetFileInfo(fileToUpload).Length
- 'C: Next, get our URL and port, and then combine them if a port was provided.
- Dim url As String = Serverpfad
- Dim port As String = ConPort
- 'If the port was provided, then insert it into the url.
- If port <> "" Then
- 'Insert the port into the Url
- 'https://www.example.com:80/directory
- Dim u As New Uri(url)
- 'Get the host (example: www.example.com)
- Dim host As String = u.Host
- 'Replace the host with the host:port
- url = url.Replace(host, host & ":" & port)
- End If
- 'C: Add the name of the file we are uploading to the end of the url
- ' This creates a "target" file name
- url = url.TrimEnd("/"c) & "/" & IO.Path.GetFileName(fileToUpload)
- 'C: Request a stream from the WebDAV server for the file we want to upload.
- 'Create the request
- Dim request As HttpWebRequest =
- DirectCast(WebRequest.Create(url), HttpWebRequest)
- 'Set the User Name and Password
- request.Credentials = New NetworkCredential(Benutzername, Passwort)
- 'Let the server know we want to "put" a file on it
- request.Method = WebRequestMethods.Http.Put
- 'Set the length of the content (file) we are sending
- request.ContentLength = fileLength
- '*** This is required for our WebDav server ***
- request.SendChunked = True
- request.Headers.Add("Translate: f")
- request.AllowWriteStreamBuffering = True
- Dim s As IO.Stream = Nothing
- Try
- 'Send the request to the server, and get the
- ' server's (file) Stream in return.
- s = request.GetRequestStream()
- Catch ex As Exception
- If MeldungenZeigen = True Then
- Console.WriteLine(ex.Message)
- Console.WriteLine("")
- End If
- End Try
- 'C: After the server has given us a stream, we can begin to write to it.
- '
- ' Note: The data is not actually being sent to the server
- ' here, it is written to a stream in memory.
- ' The data is actually sent below when the Response is retrieved
- ' from the server.
- 'Open the file so we can read the data from it
- Dim fs As New IO.FileStream(fileToUpload, IO.FileMode.Open, IO.FileAccess.Read)
- 'Create the buffer for storing the bytes read from the file
- Dim byteTransferRate As Integer = 1024
- Dim bytes(byteTransferRate - 1) As Byte
- Dim bytesRead As Integer = 0
- Dim totalBytesRead As Long = 0
- 'Read from the file and write it to the server's stream.
- Do
- 'Read from the file
- bytesRead = fs.Read(bytes, 0, bytes.Length)
- If bytesRead > 0 Then
- totalBytesRead += bytesRead
- 'Write to stream
- s.Write(bytes, 0, bytesRead)
- End If
- Loop While bytesRead > 0
- 'Close the server stream
- s.Close()
- s.Dispose()
- s = Nothing
- 'Close the file
- fs.Close()
- fs.Dispose()
- fs = Nothing
- 'C: Although we have finished writing the file to the stream, the file
- ' has not been uploaded yet. If we exited here without continuing,
- ' the file would not be uploaded.
- 'C: Now we have to send the data to the server
- Dim response As HttpWebResponse = Nothing
- Try
- '*** Send the data to the server
- ' Note: When we get the response from the server, we
- ' are actually sending the data to the server, and receiving
- ' the server's response to it in return.
- '
- ' If we did not perform this step, the file would not be uploaded.
- response = DirectCast(request.GetResponse(), HttpWebResponse)
- 'C: Finally, after we have uploaded the file, perform a little
- ' validation just to make sure everything worked as expected.
- 'Get the StatusCode from the server's Response
- Dim code As HttpStatusCode = response.StatusCode
- 'Close the response
- response.Close()
- response = Nothing
- 'Validate the uploaded file:
- ' Check the totalBytesRead and the fileLength. Both must be an exact match.
- '
- ' Check the StatusCode from the server and make sure the file was "Created"
- ' Note: There are many different possible status codes. You can choose
- ' which ones you want to test for by looking at the "HttpStatusCode" enumerator.
- If totalBytesRead = fileLength AndAlso
- code = HttpStatusCode.Created Then
- If MeldungenZeigen = True Then
- Console.WriteLine("Die XML-Datei wurde erfolgreich hochgeladen!")
- Console.WriteLine("")
- End If
- Else
- If MeldungenZeigen = True Then
- Console.WriteLine("Es ist ein Fehler beim Upload der Datei aufgetreten")
- Console.WriteLine("")
- CMDVerzögertSchließen = True
- End If
- End If
- Return True
- Catch ex As Exception
- If MeldungenZeigen = True Then
- Console.WriteLine(ex.Message)
- Console.WriteLine("")
- End If
- Return False
- End Try
- End Function
Würde mich über eure Hilfe freuen
Der Vorteil der Intelligenz besteht darin, sich dumm stellen zu können. Das Gegenteil davon ist schon schwieriger.