以下经典ASP代码不起作用 - 它返回HTTP状态500(内部服务器错误):
<%
Dim sFile, oStream
sFile = Request.QueryString("File")
Set oStream = Server.CreateObject("ADODB.Stream")
oStream.Type = 1 ' //adTypeBinary '
oStream.Open
oStream.Write Request.BinaryRead(Request.TotalBytes)
oStream.SaveToFile sFile, 2 ' //adSaveCreateOverWrite '
oStream.Close
Set oStream = Nothing
Response.Write sFile
%>
如果我注释掉 oStream.SaveToFile 行,它会工作:HTTP状态200并返回文件名(在这种情况下为C:\ testImage.jpg),但显然文件实际上并不存在写完。
我做错了什么?
这是有效的:
Const maxRead = 200000
Dim sFile, oRead, oWrite, sBuff, iBytes
on error resume next
iBytes = maxRead
sData = Request.BinaryRead(iBytes)
sData = midb(sData, 1, lenb(sData))
Do Until iBytes < 1
sBuff = Request.BinaryRead(iBytes)
If iBytes > 0 then
sData = sData & midb(sBuff, 1, lenb(sBuff))
End If
Loop
on error goto 0
Set oRead = Server.CreateObject("ADODB.Stream")
oRead.Type = 2 ' adTypeTest
oRead.Open
oRead.WriteText(sData)
Set oWrite = Server.CreateObject("ADODB.Stream")
oWrite.Type = 1 ' adTypeBinary
oWrite.Open
oRead.Position = 2 ' 2? Why 2? Don't know, but it works!
oRead.CopyTo oWrite
oWrite.SaveToFile sFile, 2 ' adSaveCreateOverWrite