moogliber
Messages postés40Date d'inscriptionlundi 26 septembre 2005StatutMembreDernière intervention 5 janvier 2012
-
12 déc. 2011 à 12:20
moogliber
Messages postés40Date d'inscriptionlundi 26 septembre 2005StatutMembreDernière intervention 5 janvier 2012
-
5 janv. 2012 à 23:16
Bonjour à tous!
J'ai une application qui télécharge un fichier sur mon site grace à l'instruction DownloadFile, voici la portion de code qui effectue cette opération :
''''on défini le chemin du fichier à créer (fichier téléchargé) :
'C:\Users\[User name]\AppData\Local\FichierCible.ext
Dim Fichier_A_Creer as string = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) & "\FichierCible.ext"
''''On télécharge
Dim WEB_CLIENT As New System.Net.WebClient()
WEB_CLIENT.DownloadFile("http://www.MonSite.fr/fichierACharger", Fichier_A_Creer)
Cela fonctionne parfaitement sur mes 3 PC tests (XP, Vista et 7) et chez la plupart des clients qui utilisent mon appli.
Le problème est que chez certaines personnes (environ 1 sur 10), l'instruction WEB_CLIENT.DownloadFile génére l'erreur :
"Une exception s'est produite lors d'une demande WebClient"
Je en sais pas si le plantage est dû à la lecture (demande de télechargement) ou à l'écriture (enregistrement du fichier sur le PC client)
J'ai tendance à penser que cela vient de l'écriture (problème de droit sur AppData\Local sur certains PC ?) mais sans en être sûr, et surtout je ne vois pas comment solutionner car le dossier AppData\Local est normalement accessible sans restriction.
Si le problème vient du téléchargement je ne comprends pas pourquoi cela marcherait sur certains PC ? serait-il nécessaire de définir un USER_AGENT ?
Des idées des pistes ?
Merci d'avance pour vos réponses
A voir également:
Erreur lors de tentative de téléchargement ou écriture d'un fichier
Mayzz
Messages postés2813Date d'inscriptionmardi 15 avril 2003StatutMembreDernière intervention 2 juin 202030 14 déc. 2011 à 04:16
Salut,
Ah le managé... Plus c'est simplifié et plus sérier les erreurs devient difficile. Je te conseille d'utiliser les classes httpWebRequest, httpWebResponse, qui fonctionnent un peu comme XMLWebRequest d'ajax.
Voici un code pour t'aiguiller, c'est une classe permettant de télécharger avec progression. Mais attention c'est synchrone.
Public Class Download
Public Event DownloadProgress(sender As Object, e As DownloadProgressEventArgs)
Public Event DownloadCompleted(sender As Object, e As System.EventArgs)
Public Event DownloadEchec(sender As Object, e As System.EventArgs)
Public Event DownloadCancelled(sender As Object, e As System.EventArgs)
Public Event DownloadError(sender As Object, e As System.EventArgs)
Public Property LastException As Exception = Nothing
Public Property _Abort As Boolean = False
Public Sub Abort()
_Abort = True
End Sub
Public Function Download(url As String, targetFile As String)
_Abort = False
Dim _localStream As IO.Stream = Nothing
Dim _remoteStream As IO.Stream = Nothing
Try
Dim request As HttpWebRequest = HttpWebRequest.Create(url)
With request
.Method = "GET"
End With
Dim response As HttpWebResponse = request.GetResponse
If response.StatusCode = HttpStatusCode.OK Then
Dim length As Long = response.ContentLength
_remoteStream = response.GetResponseStream
Dim br As New IO.BinaryReader(_remoteStream)
If IO.File.Exists(targetFile) Then
Try
IO.File.Delete(targetFile)
Catch ex As Exception
LastException = ex
RaiseEvent DownloadError(Me, New EventArgs)
Return False
End Try
End If
_localStream = IO.File.Create(targetFile)
Dim bw As New IO.BinaryWriter(_localStream)
Dim byteReceived As Long = 0
While byteReceived <> length
Dim buffer(8191) As Byte
Dim bytesRead As Integer = br.Read(buffer, 0, 8192)
bw.Write(buffer, 0, bytesRead)
byteReceived += bytesRead
Dim percent As Double = Math.Round(((byteReceived / length) * 100), 2)
Dim eventArg As New DownloadProgressEventArgs(percent)
RaiseEvent DownloadProgress(Me, eventArg)
If eventArg.Cancel Or _Abort Then
_localStream.Close()
_localStream.Dispose()
IO.File.Delete(targetFile)
RaiseEvent DownloadCancelled(Me, New EventArgs)
Return False
End If
End While
RaiseEvent DownloadCompleted(Me, New EventArgs)
Return True
Else
RaiseEvent DownloadEchec(Me, New EventArgs)
Return False
End If
Catch ex As Exception
LastException = ex
Return False
Finally
If _localStream IsNot Nothing Then
_localStream.Close()
_localStream.Dispose()
End If
If _remoteStream IsNot Nothing Then
_remoteStream.Close()
_remoteStream.Dispose()
End If
End Try
End Function
End Class
Public Class DownloadProgressEventArgs
Inherits System.EventArgs
Public Property Cancel As Boolean = False
Public Property percent As Double = 0
Public Sub New()
End Sub
Public Sub New(percent As Double)
_percent = percent
End Sub
End Class
Si le déboguage est l'art d'enlever les bogues, la programmation doit être l'art de les créer.