通过multipart-HTML-Post上传pdf确实会更改文件 [英] Upload pdf via multipart-HTML-Post does change file

查看:76
本文介绍了通过multipart-HTML-Post上传pdf确实会更改文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I try to upload a pdf via multipart with vba using this code:

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 ++进行比较时,我看到有区别. 文本"部分似乎相同,但数据"部分似乎已更改.

everything seems fine, but when I try to open the uploaded file, the pdf-reader tells me the file has a password. When I compare the files with notepad++ I can see that there is a difference. The "text part" seems to be identical but the "data"-part seems to have changed.

这是原始文件的前几行:

So this are the first few lines of the original:

%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

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

The second line is different. And the same happens with all of the content which is no text. Another example from a line in the center of the file:

原文:

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

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

So: What im doing wrong? Something releated with the encoding I suppose.

推荐答案

在用户omegastripes的帮助下,以及他对本示例的提示:

With the help of user omegastripes and his hint to this example: File updload in post form in VBS I solved my problem.

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

The content of the file has to be read and sent to the host binary (not as a string as I did)

此代码对我有用:

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

这篇关于通过multipart-HTML-Post上传pdf确实会更改文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆