|
-
Apr 21st, 2008, 10:00 PM
#1
Thread Starter
New Member
[Help me]Use winsock send picture to Http in Chinese OS
I has a project that need send picture to a website.
I used the winsock to send data to website in the client and used the .net to save the data in the server.
It was success to send picture in English os. But if the os is chinese or other OS it was unsuccess.
For it exists question in Unicode, and i can't to solve the question.
Pls give me a message about the question.
The follow is my code:
In client(the develop language is vb 6.0) include [Form1.frm], [Common.bas], [HttpRequest.bas], [UploadFile.bas]
[Form1.frm]
Private Sub Command1_Click()
'Call UploadFile("E:\atlog.txt") 'atlog.txt is english file
Call UploadFile("D:\Login.gif")
End Sub
'UploadFile function
Private Sub UploadFile(ByVal strFile As String)
Dim strSendData As String
Dim DestUrl As String
DestUrl = "/moviemaker/UploadFileWeb.aspx?Order=20070320082557"
'assign the protocol host and port
Winsock1.Protocol = sckTCPProtocol
Winsock1.RemoteHost = "10.158.96.195"
Winsock1.RemotePort = 80
strSendData = BuildFileUploadRequest(strFile, DestUrl, Winsock1.RemoteHost, Winsock1.RemotePort)
' make the connection and send the HTTP request
Winsock1.Connect
While Not blnConnected
DoEvents
Wend
Winsock1.SendData strSendData
TextRequest.Text = strSendData 'Get Requestdata , TextRequest is TextBox
End Sub
Private Sub Winsock1_Close()
blnConnected = False
Winsock1.Close
End Sub
Private Sub Winsock1_Connect()
blnConnected = True
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strdata As String
Winsock1.GetData strdata, vbString, bytesTotal
TextResponse.Text = strdata 'Get Response data , TextResponse is TextBox
End Sub
[Common.bas]
' generates a random alphanumeirc string of a given length
Public Function RandomAlphaNumString(ByVal intLen As Integer)
Dim StrReturn As String
Dim x As Integer
Dim c As Byte
Randomize
For x = 1 To intLen
c = Int(Rnd() * 127)
If (c >= Asc("0") And c <= Asc("9")) Or _
(c >= Asc("A") And c <= Asc("Z")) Or _
(c >= Asc("a") And c <= Asc("z")) Then
StrReturn = StrReturn & Chr(c)
Else
x = x - 1
End If
Next x
RandomAlphaNumString = StrReturn
End Function
'get filename form the filepath
Public Function GetFileName(ByVal strFilePath As String) As String
Dim index%
index = InStrRev(strFilePath, "\")
GetFileName = Mid$(strFilePath, index + 1, Len(strFilePath) - index)
End Function
[HttpRequest.bas]
'make body about the request
Public Function MakeBody(ByVal strFilePath As String, ByVal strBoundary As String) As String
Dim StrReturn As String
Dim strBodyStart As String
Dim strBodyEnd As String
Dim strFileName As String
strFileName = GetFileName(strFilePath)
strBodyStart = "--" & strBoundary & vbCrLf _
& "Content-Disposition: attachment; filename=""" & strFileName & """" & vbCrLf _
& "Content-Type: image/gif;Content-Transfer-Encoding: binary" & vbCrLf & vbCrLf
strBodyEnd = MakeBodyEnd(strBoundary)
MakeBody = strBodyStart & GetFileContent(strFilePath) & strBodyEnd
End Function
'make the head about the request
Public Function MakeHead(ByVal strDestURL As String, ByVal strHost As String, ByVal strPort As String, ByVal strBoundary As String, ByVal lngBodyLength As Long) As String
Dim head As String
head = "POST " & strDestURL & " HTTP/1.0" & vbCrLf _
& "Host: " & strHost & vbCrLf _
& "Content-Type: multipart/form-data, boundary=" & strBoundary & vbCrLf _
& "Content-Length: " & str(lngBodyLength) & vbCrLf & vbCrLf _
MakeHead = head
End Function
[UploadFile.bas]
'Build File Upload Request
Public Function BuildFileUploadRequest(ByVal strFilePath As String, ByVal strDestURL As String, ByVal strHost As String, ByVal strPort As String) As String
Dim strFile As String
Dim strBoundary As String
Dim strBody As String
strBoundary = RandomAlphaNumString(32)
strBody = MakeBody(strFilePath, strBoundary)
strFile = MakeHead(strDestURL, strHost, strPort, strBoundary, Len(strBody)) 'Add head
strFile = strFile & strBody 'Add Body contents
BuildFileUploadRequest = strFile
End Function
'Get file content
Public Function GetFileContent(ByVal strFilePath As String) As String
Dim StrReturn As String
Dim lngLength As Long
lngLength = FileLen(strFilePath)
StrReturn = String(lngLength, Chr(0))
On Error GoTo ERR_HANDLER
Open strFilePath For Binary Access Read Lock Write As #1
Get #1, , StrReturn
'Get File Content
GetFileContent = StrReturn
Close #1
Exit Function
ERR_HANDLER:
MsgBox Err.Description, vbCritical, "Error"
Err.Clear
End Function
In Server(The develop language is .net)
And the Server can save data and run.
protected void Page_Load(object sender, EventArgs e)
{
string strooder = Request.QueryString["Order"];
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile file = Request.Files[i];
file.SaveAs(Server.MapPath("~/saveimages/") + "\\" + strooder + "\\Upload" + "\\" + file.FileName);
Stream stream = file.InputStream;
byte[] buffer = new byte[stream.Length];
stream.Read(buffer, 0, (Int32)stream.Length);
stream.Close();
for (int j = 0; j < 10; j++)
{
Response.Write((buffer[j].ToString()));
}
}
}
I Debug find that the question from the file ANSI Unicode.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|