经典ASP,使用HTTP POST请求上传文件

时间:2016-03-25 09:05:49

标签: file-upload asp-classic http-post

我正在尝试使用经典ASP中的文件发出HTTP POST请求。目标是实现与普通HTML形式相同的方式:如下:

<html>
<head>
</head>
<body> <form method="POST" action="/upload_page.asp?category=5&subcategory=10" enctype="multipart/form-data">
  <fieldset>
  <input type="file" name="file" />
  <button type="submit">Upload</button>
  </fieldset>
</form>
</body>
</html>

这可以正常工作。如果我通过浏览器中的Web表单发出POST请求,一切都很好。

此外,这可以通过这样的cURL来完成:

curl --form file=@./test.mp4
    "/upload_page.asp?category=5&subcategory=10"

问题是,在经典ASP中尝试时我做错了什么。这是ASP代码:

Dim adTypeBinary, adTypeText, adModeReadWrite
adTypeBinary  = 1
adTypeText = 2
adModeReadWrite = 3

Function StringToBinary(input)
    dim stream
    set stream = Server.CreateObject("ADODB.Stream")
        stream.Charset = "UTF-8"
        stream.Type = adTypeText 
        stream.Mode = adModeReadWrite 
        stream.Open
        stream.WriteText input
        stream.Position = 0
        stream.Type = adTypeBinary 
        StringToBinary = stream.Read
        stream.Close
    set stream = Nothing
End Function

Function ReadBinaryFile(fullFilePath) 
    dim stream
    set stream = Server.CreateObject("ADODB.Stream")
        stream.Type = 1
        stream.Open()
        stream.LoadFromFile(fullFilePath)
        ReadBinaryFile = stream.Read()
        stream.Close
    set stream = nothing
end function 

Dim boundary
boundary = "---------------------------9849436581144108930470211272"

' Generate data to be sent with HTTP POST
Dim BINARYPOST
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = adTypeBinary 
stream.Mode = adModeReadWrite     
stream.Open
stream.Write StringToBinary("" & boundary & vbCrLf & _ 
"Content-Disposition: form-data; name=""file""; file=""" & Server.MapPath("test.mp4") & """" & _
"Content-Type: video/mp4" & vbCrLf & vbCrLf)
stream.Write ReadBinaryFile(Server.MapPath("test.mp4"))
stream.Write StringToBinary(vbCrLf & boundary & "--")
stream.Position = 0
BINARYPOST= stream.read
stream.Close

' Send HTTP POST
set xmlhttp = CreateObject("Msxml2.ServerXMLHTTP.6.0") 
xmlhttp.open "POST", upload_url, false 
xmlHttp.setRequestHeader "content-type", "multipart/form-data; boundary=" & boundary
xmlhttp.send BINARYPOST

经典ASP方法的问题是我无法在第二页上读取文件数据。 我怀疑可能服务器端页面找不到文件字段的名称(上传字段的变量名称是“文件”,就像在工作HTML示例中一样),但不确定。

知道经典ASP请求有什么问题,所以它不会像HTML和PHP版本那样发送POST请求吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

我似乎记得从ASP发布到与发布帖子的页面相同的虚拟文件夹中的URL时出现问题。尝试发布到另一台服务器上的处理程序,以验证是否可能发生这种情况。