VB的winsock实现仿HTTP文件上传
大侠帮忙下。我想在网页上实现批量上传文件,但是由于网页浏览器的限制,每次只能浏览选择一个文件,因此想用VB做个ActiveX插件嵌入到网页。由于本人是学C#的,而且也不是接触很久,不是很清楚怎样实现。百度一下了,查找到相关的资料,如下:
Private Sub Command1_Click()
Me.wskUpload.RemoteHost = "127.0.0.1 "
Me.wskUpload.RemotePort = 80
Me.wskUpload.Connect
Me.lblState = "Connecting... "
DoEvents
tmr = Timer
Do Until Me.wskUpload.State = 7
DoEvents
If Timer - tmr > = 10 Then
MsgBox "Connection Timeout. ", vbOKOnly, "Error "
Me.wskUpload.Close
Exit Sub
End If
Loop
Me.lblState = "Ready to send... "
strhttpheader = strhttpheader & "POST /WebApplication3/Upload.aspx HTTP/1.1 " & vbCrLf
strhttpheader = strhttpheader & "Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/vnd.ms-excel, application/vnd.ms-powerpoint, application/msword, application/xaml+xml, application/vnd.ms-xpsdocument, application/x-ms-xbap, application/x-ms-application, */* " & vbCrLf
strhttpheader = strhttpheader & "Referer: http://localhost/WebApplication3/Upload.aspx " & vbCrLf
strhttpheader = strhttpheader & "Accept-Language:zh-cn " & vbCrLf
strhttpheader = strhttpheader & "Content-Type: multipart/form-data; boundary=-----------------------------7d724224204ae " & vbCrLf
strhttpheader = strhttpheader & "Accept-Encoding:gzip,deflate " & vbCrLf
strhttpheader = strhttpheader & "Host: 127.0.0.1:80 " & vbCrLf
strhttpheader = strhttpheader & "User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30) " & vbCrLf
strhttpheader = strhttpheader & "Connection: Keep-Alive " & vbCrLf
strhttpheader = strhttpheader & "Cache-Control: no-cache " & vbCrLf
strhttpheader = strhttpheader & "Cookie: sjO_sid=7bRxgH; sjO_cookietime=2592000; sjO_auth=8P%2FQBlwDNE9OB%2FJ1fps51Y0lKQJpFs%2Ff9nhBEykfLBfLPISqprf%2BxUPxig; sjO_visitedfid=8D20D6D13D11D9D15D17; dnttemplateid=1; ASP.NET_SessionId=fdyo3m55pcittlmgpbbpq555 " & vbCrLf
strbody = strbody & "-----------------------------7d724224204ae " & vbCrLf
strbody = strbody & "Content-Disposition: form-data; name= " "__VIEWSTATE " " " & vbCrLf
strbody = strbody & " " & vbCrLf
strbody = strbody & "dDwxMzEzMzQxNDI3O3Q8O2w8aTwxPjs+O2w8dDxwPGw8ZW5jdHlwZTs+O2w8bXVsdGlwYXJ0L2Zvcm0tZGF0YTs+Pjs7Pjs+Pjs+J5dp6hCMH3HtqRra3FfXHNb4Aq8= " & vbCrLf
strbody = strbody & "-----------------------------7d724224204ae " & vbCrLf
strbody = strbody & "Content-Disposition: form-data; name= " "filepath " "; filename= " " " & Me.txtFilePath & " " " " & vbCrLf
strbody = strbody & "Content-Type: image/pjpeg " & vbCrLf
strbody = strbody & " " & vbCrLf
strbody2 = "-----------------------------7d724224204ae " & vbCrLf
strbody2 = strbody2 & "Content-Disposition: form-data; name= " "submit " " " & vbCrLf
strbody2 = strbody2 & " " & vbCrLf
strbody2 = strbody2 & "Submit " & vbCrLf
strbody2 = strbody2 & "-----------------------------7d724224204ae-- " & vbCrLf
strbody2 = strbody2 & " " & vbCrLf
strhttpheader = strhttpheader & "Content-Length: " & CLng(Len(strbody) + Len(strbody2) + FileLen(Me.txtFilePath)) & vbCrLf
strhttpheader = strhttpheader & " " & vbCrLf
Me.wskUpload.SendData strhttpheader & strbody
DoEvents
Me.lblState = "Sending file data... "
DoEvents
Dim bytBuff(900000) As Byte
Dim bytRem() As Byte
Open Me.txtFilePath.Text For Binary As #1
times = Int(LOF(1) / 900000)
ReDim bytRem(LOF(1) Mod 900000 - 1)
For i = 1 To times
Get #1, , bytBuff
Me.wskUpload.SendData bytBuff
DoEvents
Next
Get #1, , bytRem
Me.wskUpload.SendData bytRem
DoEvents
Close #1
Me.wskUpload.SendData " " & vbCrLf & strbody2
DoEvents
Me.lblState = "File was sent successfully. "
DoEvents
Me.wskUpload.Close
End Sub
自己在upload.aspx的页面上增加以下代码
string filepath = Server.MapPath( "./ ")+ "ProcessImages ";
HttpFileCollection uploadedFiles = Request.Files;
if( uploadedFiles.Count == 0)
{
FileInfo file = new FileInfo( "C:\\Inetpub\\wwwroot\\WebApplication3\\ProcessImages\\post1.txt ");
StreamWriter fs = file.AppendText();
fs.Write( "没有行 ");
fs.Flush();
fs.Close();
}
for (int i = 0; i < uploadedFiles.Count; i++)
{
HttpPostedFile userPostedFile = uploadedFiles[i];
try
{
string fileExt = (System.IO.Path.GetExtension(userPostedFile.FileName)).ToString().ToLower();
string fileName = System.IO.Path.GetFileName(userPostedFile.FileName);
if (userPostedFile.ContentLength <= 0 )
{
continue;
}
if (userPostedFile.ContentLength > 0 )
{
userPostedFile.SaveAs(filepath + "\\ " + fileName);
}
}
catch(Exception)
{
continue;
}
}
发现upload.aspx页面是能接收到vb中winsock控件的发送过来的数据,但是检测不到文件上传,即uploadedFiles.Count == 0
请教大侠帮忙下了,本人也没分了,望大侠见谅了
[解决办法]
发送文件内容的时候没有做 (BASE64之类的) 编码
建议使用 WINET API 递交文件,会方便很多。另外,如果网页中嵌入、有权限的话,可以直接使用 SOCKET 通讯了
[解决办法]
没看出毛病,好像 multipart/form-data没有编码一说。