webdav en dotNet
-
mardi 30 octobre 2012 11:35
Bonjour,
J'ai besoin de pouvoir manipuler des fichiers sur un serveur WebDav.
http://www.codeproject.com/Articles/36261/How-to-Download-a-File-from-a-WebDAV-Server-in-VB
J'ai testé ce code, mais le fichier jpg que je rapatrie est corrompu.
Il est moins gros que l'original...
FB
Toutes les réponses
-
mardi 30 octobre 2012 13:26
MEs sources :
'Get Url and Port Dim url As String = Me.txtUrl.Text.Trim() Dim port As String = Me.txtPort.Text.Trim() '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/fileToDownload.dat 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 'Create the request Dim request As HttpWebRequest = _ DirectCast(System.Net.HttpWebRequest.Create(url), HttpWebRequest) 'Set the User Name and Password request.Credentials = New NetworkCredential(Me.txtUserName.Text.Trim(), Me.txtPassword.Text.Trim()) request.PreAuthenticate = True request.Method = WebRequestMethods.Http.Get 'This is required for our WebDAV server request.SendChunked = True request.Headers.Add("Translate", "F") Dim response As HttpWebResponse = Nothing Try 'Get the response response = DirectCast(request.GetResponse(), HttpWebResponse) Catch ex As Exception MessageBox.Show("An error occurred while attempting to retrieve data from the remote server. " & _ "The following error occurred while attempting to retrieve the data:" & vbCrLf & vbCrLf & _ ex.Message) End Try 'Create the buffer for storing the bytes read from the server Dim byteTransferRate As Integer = 4096 '4096 bytes = 4 KB Dim bytes(byteTransferRate - 1) As Byte Dim bytesRead As Integer = 0 'Indicates how many bytes were read Dim totalBytesRead As Long = 0 'Indicates how many total bytes were read Dim contentLength As Long = 0 'Indicates the length of the file being downloaded 'This variable will reference the stream from the WebDav server Dim s As IO.Stream = Nothing 'Create a new file to write the downloaded data to Dim fs As New IO.FileStream(txtDestination.Text, IO.FileMode.Create, _ IO.FileAccess.Write) Try 'Get the stream from the server s = response.GetResponseStream() contentLength = CLng(response.GetResponseHeader("Content-Length")) pb.Minimum = 0 pb.Maximum = CInt(contentLength \ byteTransferRate) pb.Value = 0 Do 'Read from the stream bytesRead = s.Read(bytes, 0, bytes.Length) If bytesRead > 0 Then totalBytesRead += bytesRead 'Write to file fs.Write(bytes, 0, bytesRead) 'Update progress pb.Value = CInt(totalBytesRead \ byteTransferRate) If pb.Value Mod 500 = 0 Then _ Application.DoEvents() End If Loop While bytesRead > 0 Catch ex As Exception MsgBox("The following error occurred while attempting to download the file: " & vbCrLf & vbCrLf & _ ex.Message, MsgBoxStyle.Exclamation, "Error downloading file") End Try s.Close() s.Dispose() s = Nothing fs.Close() fs.Dispose() fs = Nothing 'Validate the downloaded file. Both must be an exact match ' for the file to be considered a valid download. If totalBytesRead <> contentLength Then Dim result As DialogResult = _ MessageBox.Show("The downloaded file did not download successfully, because the length of the downloaded file " & _ "does not match the length of the file on the remote site. Would you like to " & _ "delete the downloaded file?", "Download File Validate Failed", _ MessageBoxButtons.YesNo, MessageBoxIcon.Warning) If result = Windows.Forms.DialogResult.Yes Then Try 'Force the collection of 0 generation so any fs references will be released ' before attempting to delete the file GC.Collect(0) My.Computer.FileSystem.DeleteFile(txtDestination.Text) Catch ex As Exception MessageBox.Show("An error occurred while attempting to delete the following file." & vbCrLf & vbCrLf & txtDestination.Text, _ "Unable to delete file", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End If Else 'totalBytesRead = contentLength 'Files that are smaller than the byteTransferRate will not cause the ' progressbar to update If pb.Maximum = 0 Then pb.Maximum = 1 : pb.Value = 1 MessageBox.Show("The file has downloaded successfully!", "Download Complete", _ MessageBoxButtons.OK, MessageBoxIcon.Information) End If response.Close() response = NothingFB
-
mardi 30 octobre 2012 13:33
le fichier d'origine est de 28129 octet
Après l'exécution de :
response = DirectCast(request.GetResponse(), HttpWebResponse)
response.ContentLength retourne 10183
FB
-
mercredi 31 octobre 2012 11:37Propriétaire
Bonjour
Essayez d’enlever
request.SendChunked = True
Aussi, enregistrez le fichier téléchargé et vérifiez si c’est bien différente ou c’est seulement ContentLengthqui n’est pas bon.Cordialement, -
mercredi 31 octobre 2012 14:33
j'avais une erreur dans l'url...
Ou plutot, je ne prenais pas la bonne url.
...
FB
- Marqué comme réponse FrançoisBOSSANT mercredi 31 octobre 2012 14:33
- Modifié FrançoisBOSSANT mercredi 31 octobre 2012 14:33

