F A Z Z | bon bein obliger de se débrouiller... pour ceux ke sa intersse jai fait comme sa et sa marche
Code :
- Public Class UploadFormFile
- Public Shared Function PrepareDatasToSend(ByVal filepath As String, ByVal fileFormName As String, ByVal contenttype As String, ByVal querystring As Specialized.NameValueCollection, ByVal boundary As String) As Byte()
- If (fileFormName Is Nothing) OrElse (fileFormName.Length = 0) Then
- fileFormName = "file"
- End If
- If (contenttype Is Nothing) OrElse (contenttype.Length = 0) Then
- contenttype = "application/octet-stream"
- End If
- Dim Startboundary As New System.Text.StringBuilder()
- Startboundary.Append("--" )
- Startboundary.Append(boundary)
- Startboundary.Append(vbCrLf)
- Startboundary.Append("Content-Disposition: form-data; name=""" )
- Startboundary.Append(fileFormName)
- Startboundary.Append("""; filename=""" )
- Startboundary.Append(filepath)
- Startboundary.Append("""" )
- Startboundary.Append(vbCrLf)
- Startboundary.Append("Content-Type: " )
- Startboundary.Append(contenttype)
- Startboundary.Append(vbCrLf)
- Startboundary.Append(vbCrLf)
- 'ajoute les paires nom/valeurs
- Dim Endboundary As New System.Text.StringBuilder()
- If querystring IsNot Nothing Then
- Endboundary.Append(vbCrLf)
- For Each key As String In querystring.Keys
- Endboundary.Append("--" & boundary)
- Endboundary.Append(vbCrLf)
- Endboundary.Append("Content-Disposition: form-data; name=" & Chr(34) & key & Chr(34))
- Endboundary.Append(vbCrLf)
- Endboundary.Append(vbCrLf)
- Endboundary.Append(querystring.[Get](key))
- Endboundary.Append(vbCrLf)
- Next
- End If
- Endboundary.Append("--" & boundary)
- Endboundary.Append(vbCrLf)
- Dim postHeader As String = Startboundary.ToString()
- Dim postHeaderBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postHeader)
- Dim postFooter As String = Endboundary.ToString
- Dim postFooterBytes As Byte() = System.Text.Encoding.UTF8.GetBytes(postFooter)
- Dim fileBytes() As Byte = My.Computer.FileSystem.ReadAllBytes(filepath)
- Dim lenght As Integer = postHeaderBytes.Length + fileBytes.Length + postFooterBytes.Length - 1
- Dim dataPaquet(lenght) As Byte
- Array.Copy(postHeaderBytes, dataPaquet, postHeaderBytes.Length)
- Array.Copy(fileBytes, 0, dataPaquet, postHeaderBytes.Length, fileBytes.Length)
- Array.Copy(postFooterBytes, 0, dataPaquet, fileBytes.Length + postHeaderBytes.Length, postFooterBytes.Length)
- Return dataPaquet
- End Function
- End Class
|
Code :
- Sub BeginUpload()
- dim w as new webclient
- Dim querystring As New Specialized.NameValueCollection()
- 'ajouter les champs du formulaire
- querystring("nom" ) = valeur
- w.Encoding = System.Text.Encoding.UTF8
-
- 'preparer l'entete de la requete
- Dim userAgent As String = "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727)"
- Dim accept As String = "image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, */*"
- Dim accept_encoding As String = "gzip, deflate"
- Dim accept_charset As String = "ISO-8859-1,utf-8;q=0.7,*;q=0.7"
-
- Dim boundary As String = "---------------------------" + DateTime.Now.Ticks.ToString("x" )
- w.Headers.Add(HttpRequestHeader.AcceptEncoding, accept_encoding)
- w.Headers.Add(HttpRequestHeader.AcceptLanguage, "fr" )
- w.Headers.Add(HttpRequestHeader.CacheControl, "no-cache" )
- w.Headers.Add(HttpRequestHeader.KeepAlive, "true" )
- w.Headers.Add(HttpRequestHeader.ContentType, "multipart/form-data; boundary=" & boundary)
- w.Headers.Add(HttpRequestHeader.Referer, YOUPORNURLS.PORN_UPLOAD_REFERER)
- w.Headers.Add(HttpRequestHeader.UserAgent, userAgent)
- w.Headers.Add(HttpRequestHeader.Accept, accept)
- w.Headers.Add(HttpRequestHeader.AcceptCharset, accept_charset)
- dim filePath as string = "c:\exemple.mpg"
- dim MIMEtype as string = "video/mpeg" ' faire une methode pour determiner le mime type
- Dim datas() As Byte = UploadFormFile.PrepareDatasToSend(filePath, "",MIMEtype, querystring, boundary)
- w.UploadData(New Uri("http://localhost/Upload.php" ), "POST", datas)
- end sub
|
|