当前位置: 首页 > 知识库问答 >
问题:

使用multipart-form将文件上载到Salesforce

袁文景
2023-03-14

我正在尝试上传一个文本文件(也尝试了PDF等)到Salesforce。文本文件包含“Hello World”。

这是我正在使用的代码

def putFile(sf, libname, filen):
    file_name=os.path.basename(filen)
    libId=libraryExists(sf, libname)
    contentDocumentId = getContentDocumentId(sf, libname, file_name)
    if not libId:
        print(f"Provided library '{libname}' does not exists")
        return

    with open(filen, "rb") as f:
        bodyEncoded = base64.b64encode(f.read())

    boundary = '----------------------------741e90d31eff'
    headers = {
        'Content-Type' : 'multipart/form-data; boundary=' + boundary
    }

    nonBinaryPart = '--'+boundary+'\nContent-Disposition: form-data; name="entity_content";\n'
    nonBinaryPart += 'Content-Type: application/json;\r\n\r\n'
    nonBinaryPart += json.dumps({
        "ContentDocumentId" : contentDocumentId,
        "ReasonForChange" : "Large file upload", 
        "PathOnClient" : file_name
    })
    nonBinaryPart += '\r\n\r\n'

    header = '--'+boundary+'\nContent-Disposition: form-data; name="VersionData"; filename="'+file_name+'";\nContent-Type: application/octet-stream\r\n\r\n'
    footer = '--'+boundary+'--'
    headerEncoded = header
    last4Bytes = bodyEncoded[len(bodyEncoded)-4:len(bodyEncoded)]
    print(type(last4Bytes))
    print(last4Bytes)
    if last4Bytes.endswith(b'=='):
        last4Bytes = last4Bytes[0:2] + b'0K'
        bodyEncoded = bodyEncoded[0:len(bodyEncoded)-4] + last4Bytes
        footerEncoded = footer
        reqBody = headerEncoded+str(bodyEncoded)+footerEncoded
    elif last4Bytes.endswith(b'='):
        print('Ends with =')
        last4Bytes = last4Bytes[0:3] + b'N'
        bodyEncoded = bodyEncoded[0:len(bodyEncoded)-4] + last4Bytes
        footer = '\n' + footer;
        footerEncoded = footer
        reqBody = headerEncoded+str(bodyEncoded)+footerEncoded
    else:
        footer = '\r\n' + footer
        footerEncoded = footer
        reqBody = headerEncoded+str(bodyEncoded)+footerEncoded

    reqBody = nonBinaryPart + reqBody

    print('==================================================')
    print(reqBody)
    print('==================================================')

    res = sf.contentVersion.create(reqBody, headers)

    print(res)

    print('Now downloading it...')
    os.system('rm -f ' + filen + '_downloaded')
    getFile(sf, contentDocumentId, filen + '_downloaded', './' )
    print('Downloaded.')


    os.system('md5sum ' + filen)
    os.system('md5sum ' + filen + '_downloaded')

这将导致以下看起来符合Salesforce指导原则的请求正文:https://developer.Salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/dome_sobject_insert_update_blob.htm

内容-类型:多部分/表单-数据;boundary=“-----------------------741E90D31EFF”Accept:Application/JSON

请求正文:

------------------------------741E90D31EFF内容-处置:表单-数据;name=“Entity_Content”;内容类型:Application/JSON;

共有1个答案

亢雅懿
2023-03-14

就像您的代码显示bodyencode=base64.b64encode(f.read()),文件是以base64编码的方式发送的。您需要在下载文件后对其进行解码,以恢复其原始的“可读”值。

注意:就像您的评论所说的,您的文件的内容是B'AGVSBG8GD29YBGQK',其中B表示一个base64编码的字符串,另一部分是编码值,您也可以使用base64decode这样的在线工具对其进行编码,它将显示该字符串正是Hello World

 类似资料:
  • 问题内容: 我正在尝试使用伪装完成多部分文件上传,但似乎在任何地方都找不到很好的示例。我本质上希望HTTP请求类似于以下内容: 甚至… 我是否需要手动构建请求主体,包括生成多部分边界?考虑到此客户端可以执行的其他所有操作,这似乎有点过头了。 问题答案: 不,你没有。您只需要定义一种代理接口方法,将content-type指定为:multipart / form- data和其他信息,例如远程API

  • 我试图使用feign完成一个多部分文件上传,但是我似乎在任何地方都找不到一个好的例子。我基本上希望HTTP请求的结果类似如下: 或者甚至… 我需要手动构建请求体吗,包括生成多部分边界?考虑到这个客户端可以做的所有其他事情,这似乎有点过分。

  • 我有一个用例,我需要通过AWS API网关使用AngularJS应用程序将CSV文件上传到我的后端应用程序(运行在Elastic Beanstalk-Django Python REST应用程序中)。 API网关与AWS Cognito集成。 后端应用程序需要CSV上载的表单数据。 重要设置在API网关资源POST方法中完成 API网关->设置->二进制媒体类型-添加了‘多部分/表单-数据 将标题

  • 我正在尝试使用curl向REST服务发布一个xml文件(utf-16编码)。REST服务需要“multipart/form-data”内容类型。 Curl脚本:Curl-k-i-h“content-type=multipart/form-data”-f“filename=@file.xml;type=text/xml”-x POST-u: 然而,我在运行脚本时得到500个内部服务器错误。 响应:<

  • 根据SkyDrive api(http://msdn.microsoft.com/en-us/library/live/hh826531.aspx#uploading_files),我在帖子请求的正文中以字节[]传递图像的内容。文件创建发生在skyDrive服务器上,但当我在skyDrive中打开它时,它说“文件应用程序被损坏或损坏”。 请求的正文 --A300x内容处理:表单数据;name=“f

  • 我尝试使用C#将文件上传到FTP服务器。文件已上传,但字节为零。