By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,066 Members | 1,841 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,066 IT Pros & Developers. It's quick & easy.

problem in upload through http winsock from non-english OS

P: 31
Hi all,
I am trying to upload file using VB HTTP Post method and winsock control . Can successfully upload the files when client machine is English OS, but create problem when upload from Chinese OS. Currently what i am doing is reading file in string and then build a html request and send these string data to upload. fuction which read a file content and buildhttp request is given below.. can anyone suggest what is the problem. is it so because i am reading file in string or send the data as string??? or anything else?? i am using MIME type as "application / octet - stream"


Expand|Select|Wrap|Line Numbers
  1.  
  2. ' this function builds a http request bases on the following parameters:
  3. ' data = the data from the file to be uploaded
  4. ' DestUrl = a URL to containing information on where to send the data
  5. ' UploadName = the field upload name usually pass by <input type="file" name="uploadname"
  6. ' Filename = the name of the file
  7. ' The MIME type of the file
  8. Public Function BuildFileUploadRequest(ByRef strData As String, _
  9.                                         ByRef DestUrl As URL, _
  10.                                         ByVal UploadName As String, _
  11.                                         ByVal FileName As String, _
  12.                                         ByVal MimeType As String) As String
  13.  
  14.     Dim strHttp As String ' holds the entire HTTP request
  15.     Dim strBoundary As String 'the boundary between each entity
  16.     Dim strBody As String ' holds the body of the HTTP request
  17.     Dim lngLength As Long ' the length of the HTTP request
  18.  
  19.  
  20.     ' create a boundary consisting of a random string
  21.     strBoundary = RandomAlphaNumString(32)
  22.  
  23.     ' create the body of the http request in the form
  24.     '
  25.     ' --boundary
  26.     ' Content-Disposition: form-data; name="UploadName"; filename="FileName"
  27.     ' Content-Type: MimeType
  28.     '
  29.     ' file data here
  30.     '--boundary--
  31.     strBody = "--" & strBoundary & vbCrLf
  32.     strBody = strBody & "Content-Disposition: form-data; name=""" & UploadName & """; filename=""" & _
  33.                     FileName & """" & vbCrLf
  34.     strBody = strBody & "Content-Type: " & MimeType & vbCrLf '";CharSet=utf-8"
  35.     strBody = strBody & vbCrLf & strData
  36.     strBody = strBody & vbCrLf & "--" & strBoundary & "--"
  37.  
  38.  
  39.     ' find the length of the request body - this is required for the
  40.     ' Content-Length header
  41.      lngLength = Len(strBody)
  42.  
  43.     ' construct the HTTP request in the form:
  44.     '
  45.     ' POST /path/to/reosurce HTTP/1.0
  46.     ' Host: host
  47.     ' Content-Type: multipart-form-data;charset=utf-8, boundary=boundary
  48.     ' Content-Length: len(strbody)
  49.     '
  50.     ' HTTP request body
  51.     strHttp = "POST " & DestUrl.URI & "?" & DestUrl.Query & " HTTP/1.0" & vbCrLf
  52.     strHttp = strHttp & "Host: " & DestUrl.Host & vbCrLf
  53.     strHttp = strHttp & "Content-Type: multipart/form-data ;charset=utf-8, boundary=" & strBoundary & vbCrLf
  54.     strHttp = strHttp & "Content-Length: " & lngLength & vbCrLf & vbCrLf
  55.     strHttp = strHttp & strBody
  56.  
  57.     'Debug.Print LenB(strBoundary), LenB(strData), LenB(strBody), LenB(strHttp)
  58.  
  59.  
  60.     BuildFileUploadRequest = strHttp
  61.  
  62. End Function
  63.  
  64. ' this function retireves the contents of a file and returns it as a string
  65. ' this is also ture for binary files
  66. Public Function GetFileContents(ByVal strPath As String) As String
  67.     Dim StrReturn As String
  68.     Dim lngLength As Long
  69.  
  70.  
  71. On Error GoTo ERR_HANDLER
  72.  
  73.     lngLength = FileLen(strPath)
  74.  
  75.     StrReturn = String(lngLength, Chr(0))
  76.  
  77.     Open strPath For Binary As #1
  78.  
  79.     Get #1, , StrReturn
  80.  
  81.     GetFileContents = StrReturn
  82.  
  83.     Close #1
  84.  
  85.     Exit Function
  86.  
  87. ERR_HANDLER:
  88.     MsgBox Err.Description, vbCritical, "ERROR"
  89.  
  90.     Err.Clear
  91. End Function
Sep 21 '07 #1
Share this question for a faster answer!
Share on Google+

Post your reply

Sign in to post your reply or Sign up for a free account.