我正在尝试将二进制文件上传到Flask端点,而不使用任何类型的multipart/form-data
。我想简单POST
或PUT
文件到终端里面的数据,并将其保存到服务器上的文件。我可以找到的唯一示例,以及在其他问题中讨论的唯一方法,都使用multipart/form-data
。
以下“有效”,但SHA256散列通常不匹配,而正常上传即可form-data
。
@application.route("/rupload/<filename>", methods=['POST', 'PUT'])
def rupload(filename):
# Sanity checks and setup skipped.
filename = secure_filename(filename)
fileFullPath = os.path.join(UPLOAD_FOLDER, filename)
with open(fileFullPath, 'wb') as f:
f.write(request.get_data())
return jsonify({
'filename': filename,
'size': os.path.getsize(fileFullPath)
})
此外,上述方法在内存方面效率很低。是否可以通过某种类型的缓冲流将其写入输出文件?谢谢!
编辑:这就是我正在测试的方式:
curl -v -H 'Content-Type: application/octet-stream' -X POST --data @test.zip https://example.com/test/rupload/test.zip
编辑: --data-binary
没有区别。
您是否尝试过使用hashlib?
import hashlib
...
@application.route("/rupload/<filename>", methods=['POST', 'PUT'])
def rupload(filename):
# Sanity checks and setup skipped.
filename = secure_filename(filename)
fileFullPath = os.path.join(UPLOAD_FOLDER, filename)
file_hash = hashlib.sha256()
with open(fileFullPath, 'wb+') as f:
input = request.get_data()
f.write(input)
file_hash.update(input)
...
fileDigest = file_hash.hexdigest()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句