HTTP Post fil (Upload fil)
Hey Eksperter!Jeg sidder med et problem - nemlig at jeg gerne vil uploade en fil ved hjælp af http post.
Jeg har søgt en del på nettet, og fundet noget der muligvis kunne fungere. Problemet er bare at jeg ikke lige kan spotte hvad det er jeg mangler, for at få selve filen uploaded?
Her er det jeg har fundet, som mit webscript modtager, men filen fylder 0 kb.
Jeg vil gerne have noget i stil med den her header:
#########################################################
POST /upload.pl? HTTP/1.0
Host: fakeachievement.com
Content-Type: multipart/form-data, boundary=OegjqNc2L6ao9HPHkf1M4HnMmY5zzev0
Content-Length: 2131
--OegjqNc2L6ao9HPHkf1M4HnMmY5zzev0
Content-Disposition: form-data; name="filename"; filename="test.txt
#########################################################
Dette er den VB.Net kode jeg har fundet, men som desværre ikke fungerer helt som jeg gerne ser den skulle gøre.
#########################################################
Public Sub PostData(ByVal strURL As String, Optional ByVal POST1 As String = "", Optional ByVal POST2 As String = "")
Try
Dim URLRes As HttpWebResponse
Dim URLReq As HttpWebRequest
' Declare a variable named client of type WebClient.
Dim myWebClient As WebClient
' Instantiate a WebClient object and assign it to the 'client' variable.
myWebClient = New WebClient
' Add handler to process theWebClient's DownloadProgressChanged events.
AddHandler myWebClient.UploadProgressChanged, AddressOf DownloadProgress
' Add handler to process theWebClient's DownloadCompleted event.
AddHandler myWebClient.UploadStringCompleted, AddressOf DownloadCompleted
' Declare a variable named downloadUri of type Uri
Dim downloadUri As Uri
' Instantiate a new Uri object and assign it to the 'downloadUri' variable.
downloadUri = New Uri(strURL)
'myWebClient.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4) Gecko/20060508 Firefox/1.5.0.4")
'myWebClient.Headers.Add("Accept-Language", "en-us,en;q=0.5")
'myWebClient.Headers.Add("Content-Length: ")
If txtproxy.Text.Length > 0 Then
Dim proxy As New WebProxy(txtproxy.Text, True)
myWebClient.Proxy = proxy
End If
'' For Intial Posting Of Data
If strURL.Length > 0 Then
myWebClient.UploadStringAsync(downloadUri, "filename=" & POST1)
End If
Try
URLReq = mywebclient.UploadStringAsync(
URLRes = URLReq.GetResponse
Catch
MsgBox(Err.Description)
End Try
Catch webex As WebException
Console.WriteLine(webex.Message)
End Try
End Sub
Public Sub DownloadProgress(ByVal sender As Object, ByVal e As UploadProgressChangedEventArgs)
Debug.Print(e.ProgressPercentage)
End Sub
Public Sub DownloadCompleted(ByVal sender As Object, ByVal e As UploadStringCompletedEventArgs)
Msgbox("Hurray!")
End Sub
#########################################################
200 points - gerne flere hvis det er lovligt iflg. reglerne!