【问题标题】:Converting JS file upload with fetch() to python使用 fetch() 将 JS 文件上传转换为 python
【发布时间】:2022-01-14 14:48:07
【问题描述】:

我必须将 js 代码转换为 python。 js 代码使用 fetch() 通过 POST 请求执行文件上传。 这是js代码:

<input type="file" />
<button onclick="upload()">Upload data</button>
<script>
          upload = async() =>
          {             
             const fileField = document.querySelector('input[type="file"]');
             await uploadDoc(fileField.files[0] );
          };
          
          uploadDoc = async( file ) =>
          {             
              let fd = new FormData();
              fd.append( 'file', file ); 
              fd.append( 'descr', 'demo_upload' );
              fd.append( 'title', name );
              fd.append( 'contentType', 'text' );
              fd.append( 'editor', user );
              let resp = await fetch( url, { method: 'POST', mode: 'cors', body: fd }); 
          };
</script>

代码有效并符合此处提供的 fetch() 文档: https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch#uploading_a_file

现在当我尝试在 python 中重新创建它时,我得到一个 500 HTTP 状态代码 这是python代码:

from urllib import request
from urllib.parse import urlencode
import json
with open('README.md', 'rb') as f:
          upload_credentials = {
            "file": f,
            "descr": "testing",
            "title": "READMEE.md",
            "contentType": "text",
            "editor": username,
          }
          url_for_upload = "" #here you place the upload URL
          
          req = request.Request(url_for_upload, method="POST")
          form_data = urlencode(upload_credentials)
          form_data = form_data.encode()
    
          response = request.urlopen(req, data=form_data)
          http_status_code = response.getcode()
          content = response.read()
          print(http_status_code)
          print(content)

但这不起作用,我收到此错误:

raise HTTPError(req.full_url, code, msg, hdrs, fp)
urllib.error.HTTPError: HTTP Error 500:

有 js 和 python 经验的人或许能看出 python 端出了什么问题,或者如何将 fetch() 函数转换为 python。

【问题讨论】:

  • 您使用的是什么 API?
  • 获取似乎是一个 multipart/form-data" 请求。也许看看这个问题stackoverflow.com/questions/12385179/…
  • @某公司网页应用的API。
  • @ikhvjs 我会调查的。谢谢!

标签: javascript python post file-upload fetch


【解决方案1】:

我认为问题在于您正在以二进制模式 (rb) 读取文件。你只需要r:

with open('README.md', 'r') as f: # r instead of rb

不过,我还是推荐requests 模块,它使用更广泛,在这种情况下更容易。

import requests # pip install requests

url = "https://www.example.com/upload_file"

headers = {
    "content-type": "text/plain" # force text file content type
}

files = {
   "my_file": ("FILE_NAME.md", open("README.md","r")) # tuple containing file name, and io.BytesIO file buffer
}

data = { # in case you want to send a request payload too
    "foo": "bar"
}

r = requests.post(url, headers=headers, files=files, data=data)

if r.status_code == 200:
    print(r.text) # print response as a string (r.content for bytes, if response is binary like an image)

【讨论】:

  • 非常感谢!关于r 而不是rb 的第一个想法不起作用。在您的第二个实现中,我应该将键值对放在我的代码中名为upload_credentials 的字典中的哪个位置?您能否修改添加这些参数的代码示例,以便我知道将它们放在哪里?它在名为data 的字典中吗?
  • 您要上传多部分/表单数据负载吗?文件参数就像标题一样,除了它是表单数据:2.python-requests.org/en/master/user/quickstart/…。我的代码中的元组是一个示例,说明如何在选择要上传的文件的名称时附加二进制文件。
猜你喜欢
  • 2020-06-28
  • 2017-06-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-22
  • 1970-01-01
相关资源
最近更新 更多