为什么docx文件被二进制post破坏了,但是.doc和.pdf是可以的?
我正在以二进制格式将文件发布到API .pdf和.doc文件很好-它们按预期到达系统,打开时没有任何问题 但由于某些原因,.docx文件显示为已损坏 为什么会这样为什么docx文件被二进制post破坏了,但是.doc和.pdf是可以的?,pdf,asp-classic,binary,http-post,docx,Pdf,Asp Classic,Binary,Http Post,Docx,我正在以二进制格式将文件发布到API .pdf和.doc文件很好-它们按预期到达系统,打开时没有任何问题 但由于某些原因,.docx文件显示为已损坏 为什么会这样 Sub PostTheFile(CVFile, fullFilePath, PostToURL) strBoundary = "---------------------------9849436581144108930470211272" strRequestStart = "--" & strBounda
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
更新:
添加到流中。如所指出的那样关闭。我完全期待它能解决问题,但它没有:(
更新2:
我一直在用不同的流模式和编码进行测试,但我尝试的任何东西都没有给我带来任何乐趣
我还试着调试DOCX文档。我已经检查了文档中的所有xml文件,寻找无效的xml-我想这可能会给我一个错误的线索,但结果都是有效的
读取二进制文件后未关闭流
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
docx文件的文件类型是“application/vnd.openxmlformats officedocument.wordprocessingml.document”。
因此,您可以通过为数据源表中的数据类型定义nvarchar(max)来解决此问题。谢谢您,meda,我完全没有注意到这一点。尽管它们仍然被认为是损坏的,但肯定还有另一个问题:(我如何测试你的文件?你把全部代码都放在你的帖子里了吗?是的,那就是全部代码。有几个函数可以获取文件/目录路径等。但是当我让它工作时,我只是硬编码了这些值。不同的问题,关于同一个问题: