我正在以二进制格式将文件发布到API。
.pdf和.doc文件很好——它们按预期到达系统,打开时没有任何问题。
但由于某些原因,.docx文件显示为已损坏。
为什么会这样?
Sub PostTheFile(CVFile, fullFilePath, PostToURL)
strBoundary = "---------------------------9849436581144108930470211272"
strRequestStart = "--" & strBoundary & vbCrlf &_
"Content-Disposition: attachment; name=""file""; filename=""" & CVFile & """" & vbcrlf & vbcrlf
strRequestEnd = vbCrLf & "--" & strBoundary & "--"
Set stream = Server.CreateObject("ADODB.Stream")
stream.Type = adTypeBinary '1
stream.Mode = adModeReadWrite '3
stream.Open
stream.Write StringToBinary(strRequestStart)
stream.Write ReadBinaryFile(fullFilePath)
stream.Write StringToBinary(strRequestEnd)
stream.Position = 0
binaryPost = stream.read
stream.Close
Set stream = Nothing
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
httpRequest.Open "PATCH", PostToURL, False, "username", "pw"
httpRequest.setRequestHeader "Content-Type", "multipart/form-data; boundary=""" & strBoundary & """"
httpRequest.Send binPost
Response.write "httpRequest.status: " & httpRequest.status
Set httpRequest = Nothing
End Sub
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
更新:
在Stream中添加。如前所述关闭。我完全希望这能解决问题,但事实并非如此:(
更新2:
我一直在测试不同的流模式和编码,但我所尝试的一切都没有给我带来任何乐趣。
我还尝试过调试DOCX文档。我已经浏览了文档中的所有xml文件,寻找无效的xml——我想这可能会让我知道哪里出了问题,但结果都是有效的。
如何调试损坏的docx文件?
docx文件的文件类型为"application/vnd.openxmlformats officedocument.wordprocessingml.docal"。因此,您可以通过在数据源表中为数据类型定义nvarchar(max)来解决这个问题。
读取二进制文件后没有关闭流
function ReadBinaryFile(fullFilePath)
dim stream
set stream = Server.CreateObject("ADODB.Stream")
stream.Type = 1
stream.Open()
stream.LoadFromFile(fullFilePath)
ReadBinaryFile = stream.Read()
stream.Close 'here
set stream = nothing
end function