【发布时间】:2014-07-14 17:11:53
【问题描述】:
我已经坚持了几天了,没有任何进展。使用经典 ASP,我需要获取一个上传的 .JPG 文件(来自 html 表单输入 type='file')并对其进行 base64 编码,以便我可以将其发送到 Java Web 服务。 Java Web 服务只是将图像(在 SQL 图像字段中)存储在数据库中。我认为转换为 BASE64 是在 xml 中传输参数的最佳方式。到目前为止,这是我所拥有的:
HTML:
<label>Upload Picture</label>
<input name="file" id="file" type="file" size=40 /> <br />
平均售价:
Dim load
Set load = new Loader
load.initialize
Dim fileData
fileData = load.getFileData("file")
Dim fileName
fileName = LCase(load.getFileName("file"))
Dim fileSize
fileSize = load.getFileSize("file")
Dim objXML
Dim objNode
Dim strB64
Set objXML = Server.CreateObject("MSXML2.DomDocument.3.0")
Set objNode = objXML.createElement("base64")
objNode.dataType = "bin.base64" 'stores binary as base64 string
objNode.nodeTypedValue = fileData 'binary value
strB64 = objNode.Text
装载机:
加载器有很多代码(如果需要,我可以在这里复制所有代码)。它本质上从请求中获取所有字节并将它们解析为字典对象。以下是它获取数据的方式:
Class Loader
Private dict
Private Sub Class_Initialize
Set dict = Server.CreateObject("Scripting.Dictionary")
End Sub
Public Sub Initialize
If Request.TotalBytes > 0 Then
Dim binData
binData = Request.BinaryRead(Request.TotalBytes)
getData binData
End If
End Sub
Public Function getFileData(name) '"file"
If dict.Exists(name) Then
getFileData = dict(name).Item("Value")
Else
getFileData = ""
End If
End Function
如果需要,我可以发布解析二进制文件并将其存储在字典中的子程序。
上面的代码给出了这个错误:
msxml3.dll error '80004005'
Error parsing '????' as bin.base64 datatype.
在这一行:
objNode.nodeTypedValue = fileData 'binary value
更新:
这里是数据被加载到字典的地方:
Private Sub getData(rawData)
Dim separator
separator = MidB(rawData, 1, InstrB(1, rawData, ChrB(13)) - 1)
Dim lenSeparator
lenSeparator = LenB(separator)
Dim currentPos
currentPos = 1
Dim inStrByte
inStrByte = 1
Dim value, mValue
Dim tempValue
tempValue = ""
While inStrByte > 0
inStrByte = InStrB(currentPos, rawData, separator)
mValue = inStrByte - currentPos
If mValue > 1 Then
value = MidB(rawData, currentPos, mValue)
Dim begPos, endPos, midValue, nValue
Dim intDict
Set intDict = Server.CreateObject("Scripting.Dictionary")
begPos = 1 + InStrB(1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))
nValue = endPos
Dim nameN
nameN = MidB(value, begPos, endPos - begPos)
Dim nameValue, isValid
isValid = True
If InStrB(1, value, stringToByte("Content-Type")) > 1 Then
begPos = 1 + InStrB(endPos + 1, value, ChrB(34))
endPos = InStrB(begPos + 1, value, ChrB(34))
If endPos = 0 Then
endPos = begPos + 1
isValid = False
End If
midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "FileName", trim(byteToString(midValue))
begPos = 14 + InStrB(endPos + 1, value, stringToByte("Content-Type:"))
endPos = InStrB(begPos, value, ChrB(13))
midValue = MidB(value, begPos, endPos - begPos)
intDict.Add "ContentType", trim(byteToString(midValue))
begPos = endPos + 4
endPos = LenB(value)
nameValue = MidB(value, begPos, ((endPos - begPos) - 1))
Else
nameValue = trim(byteToString(MidB(value, nValue + 5)))
End If
If isValid = True Then
intDict.Add "Value", nameValue
intDict.Add "Name", nameN
dict.Add byteToString(nameN), intDict
End If
End If
currentPos = lenSeparator + inStrByte
Wend
End Sub
Private Function stringToByte(toConv)
Dim tempChar, i
For i = 1 to Len(toConv)
tempChar = Mid(toConv, i, 1)
stringToByte = stringToByte & chrB(AscB(tempChar))
Next
End Function
Private Function byteToString(toConv)
dim i
For i = 1 to LenB(toConv)
byteToString = byteToString & chr(AscB(MidB(toConv,i,1)))
Next
End Function
【问题讨论】:
-
你能说明你把二进制数据 in 放到字典对象的什么地方吗?
-
我找到了我的问题的答案并将其发布在下面。为了后代(以及那些只是好奇的人),我还添加了加载到上面字典的方法。谢谢!
-
我想你错过了从 MultiByte 到
VT_UI1 | VT_ARRAY的转换,这就是为什么我想看看你是如何将数据放入字典的。很高兴您能够自己解决。
标签: file-upload vbscript asp-classic binary base64