我将文件以二进制格式发布到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所述在Stream.Close中添加。我完全希望能解决这个问题,但事实并非如此:(
更新2:
我一直在测试不同的流模式和编码,但我尝试的任何东西都没有给我任何快乐。
我也尝试过调试DOCX文档。我已经浏览了文档中的所有xml文件,寻找无效的xml - 我想这可能会给我一个关于它出错的地方的线索,但这一切都是有效的。
答案 0 :(得分:1)
docx文件的文件类型是" application / vnd.openxmlformats-officedocument.wordprocessingml.document"。 因此,您可以通过为数据源表中的数据类型定义nvarchar(max)来解决此问题。
答案 1 :(得分:0)
您在阅读二进制文件后没有关闭流
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