通过multipart-HTML-Post上传pdf确实会更改文件

时间:2018-09-18 14:39:55

标签: vba http file-upload

我尝试使用以下代码通过带有vba的多部分上传pdf文件:

Public Function sap_upload(ByVal par_objectID As String, ByVal par_description As String, ByVal par_filename As String) As Integer

    Dim ls_param As String
    Dim text As String
    Dim line As String
    Dim url As String
    Dim web As MSXML2.XMLHTTP60
    url = "http://someurl.xml"


    Set web = CreateObject("MSXML2.XMLHTTP")


    Call web.Open("POST", url, False)
    Const Boundary As String = "AaB03x"
    Call web.setRequestHeader("content-type", "multipart/form-data;boundary=" & Boundary)
    Call web.setRequestHeader("Connection", "Keep-Alive")
    Call web.setRequestHeader("cache-control", "no-cache")


    Dim objStream, strData

    Set objStream = CreateObject("ADODB.Stream")

    objStream.Charset = "utf-8"
    objStream.Open
    objStream.LoadFromFile (par_filename)

    strData = objStream.ReadText()

    Dim getFileResult
    getFileResult = GetFile(par_filename)

    ls_param = vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""object_id""" & vbNewLine & vbNewLine & par_objectID & _
             vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""description""" & vbNewLine & vbNewLine & par_description & _
             vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""file""; filename=""" & par_filename & """" & vbNewLine & _
             vbNewLine & strData & vbNewLine & vbNewLine & "--" & Boundary & "--" & vbNewLine

    Call web.Send(ls_param)

end function

一切似乎都很好,但是当我尝试打开上传的文件时,pdf阅读器会告诉我该文件具有密码。当我将文件与notepad ++进行比较时,我看到有区别。 “文本”部分似乎是相同的,但“数据”部分似乎已更改。

这是原始文件的前几行:

%PDF-1.6
%âãÏÓ
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj

这是上传的文件:

%PDF-1.6
%����
37 0 obj <</Linearized 1/L 20597/O 40/E 14115/N 1/T 19795/H [ 1005 215]>>
endobj

第二行是不同的。对于没有文本的所有内容,也会发生同样的情况。文件中心一行的另一个示例:

原文:

s†fŸ«¸"$ ºƒŸ44}2šÔ@Y•¨×Ç,(ŒA-$ÈÇÝŠëâÓˆea‰,Òs<W²«äÒv{ r8¸ o*=ËîÁ—œ   5´xÎ&:‘Š‚2bÁnu:˜²ºú/nâ¼æ·ig–£‘±Åô3]E

已上传的文件:

s�f���"$ ���44}2��@Y����,(�A-$��݊��ӈea�,�s<W����v{ r8� o*=����� 5�x�&:���2b�nu:���/n���ig������3]E

所以:我在做什么错?我想与编码有关的东西。

1 个答案:

答案 0 :(得分:2)

借助用户omegastripes和他对本示例的提示:File updload in post form in VBS我解决了我的问题。

必须读取文件的内容并将其发送到主机二进制文件(而不是像我那样作为字符串)

此代码对我有用:

Public Function sap_addTest(ByVal par_objectID As String, ByVal par_description As String, ByVal par_filename As String) As Integer

  Dim ls_param As String
  Dim text As String
  Dim line As String
  Dim url As String
  Dim web As MSXML2.XMLHTTP60
  url = "http://someurl.xml"


  Set web = CreateObject("MSXML2.XMLHTTP")


  Call web.Open("POST", url, False)
  Const Boundary As String = "AaB03x"
  Call web.setRequestHeader("content-type", "multipart/form-data;boundary=" & Boundary)
  Call web.setRequestHeader("ws-callingapplication", sys_db)
  Call web.setRequestHeader("Connection", "Keep-Alive")
  Call web.setRequestHeader("cache-control", "no-cache")


  Dim baBuffer() As Byte

  Dim bytData
  Dim bytPayLoad

  With CreateObject("ADODB.Stream")
       .Type = 1
       .Mode = 3
       .Open
       .LoadFromFile par_filename
       bytData = .Read
   End With

   With CreateObject("ADODB.Stream")
      .Mode = 3
      .Charset = "Windows-1252"
      .Open
      .Type = 2
      .WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""object_id""" & vbNewLine & vbNewLine & par_objectID
      .WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""description""" & vbNewLine & vbNewLine & par_description
      .WriteText vbNewLine & "--" & Boundary & vbNewLine & "Content-Disposition: form-data; name=""file""; filename=""" & par_filename & """" & vbNewLine
      .WriteText vbNewLine
      .Position = 0
      .Type = 1
      .Position = .Size
      .Write bytData
      .Position = 0
      .Type = 2
      .Position = .Size
      .WriteText vbNewLine & vbNewLine & "--" & Boundary & "--" & vbNewLine
      .Position = 0
      .Type = 1
      bytPayLoad = .Read
  End With


  Call web.Send(bytPayLoad)

  'Debug.Print web.status
  'Debug.Print web.responseText

End Function