使用XMLHttpRequest上传图像(multipart / form-data)

时间:2016-04-01 14:08:33

标签: image file-upload xmlhttprequest httprequest uploader

我想为网站制作上传者 ..我使用 Fiddler 获取有关请求的信息,我试图制作它...一切都很相似,我只是不知道如何将图像放入请求中,我发现了一些关于如何使用 HTMLRequest 执行此操作的代码,但是他们没有帮助我.. 这是我的代码:

Const BOUNDARY = "----WebKitFormBoundary0IFvhsx4WYUNnJ1s"
        Dim strResponse, xmlhttp
        Dim strData
        strData = ""
        strData = strData & "--" & BOUNDARY & vbCrLf
        strData = strData & "Content-Disposition: form-data; name=""utf8""" & vbCrLf & vbCrLf
        strData = strData & "✓"
        strData = strData & vbCrLf
        strData = strData & "--" & BOUNDARY & vbCrLf
        strData = strData & "Content-Disposition: form-data; name=""authenticity_token""" & vbCrLf & vbCrLf
        strData = strData & "N+Om0R4Znca+WV/68i2kBRCHB5spBi+s5KIYMbG5/6c="
        strData = strData & vbCrLf
        strData = strData & "--" & BOUNDARY & vbCrLf
        strData = strData & "Content-Disposition:  form-data; name=""file""; filename=""img.png""" & vbCrLf
        strData = strData & "Content-Type: image/png" & vbCrLf & vbCrLf
        strData = strData & "The image should be here"
        xmlhttp = CreateObject("Microsoft.XMLHTTP")
        xmlhttp.open("POST", "http://*/upload", False)
        xmlhttp.SetRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8")
        xmlhttp.SetRequestHeader("Accept-Language", "fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4")
        xmlhttp.SetRequestHeader("Accept-Encoding", "gzip, deflate")
        xmlhttp.SetRequestHeader("User-agent", "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/48.0.2564.97 Safari/537.36")
        xmlhttp.SetRequestHeader("Content-Type", "multipart/form-data; boundary=" & BOUNDARY)
        xmlhttp.Send(strData)
        If xmlhttp.readyState = 4 Then
            strResponse = xmlhttp.responseText
            MsgBox(strResponse)
        End If

如果您知道该怎么做,请帮助我,我将不胜感激,谢谢!

0 个答案:

没有答案