[英]How to send file from Nodejs to Flask Python?
Hope you are doing well.希望你一切顺利。 I'm trying to send pdfs file from Nodejs to Flask using Axios.
我正在尝试使用 Axios 将 pdfs 文件从Nodejs发送到Flask 。 I read files from a directory (in the form of buffer array) and add them into formData (an npm package) and send an Axios request.
我从一个目录(以缓冲区数组的形式)读取文件并将它们添加到 formData(一个 npm 包)中并发送一个 Axios 请求。
const existingFile = fs.readFileSync(path)
console.log(existingFile)
const formData = new nodeFormData()
formData.append("file", existingFile)
formData.append("fileName", documentData.docuName)
try {
const getFile = await axios.post("http://127.0.0.1:5000/pdf-slicer", formData,
{
headers: {
...formData.getHeaders()
}
})
console.log(getFile)} catch (e) {console.log(e, "getFileError")}
On flask side:在烧瓶方面:
I'm trying to get data from the request.我正在尝试从请求中获取数据。
print(request.files)
if (request.method == "POST"):
file=request.form["file"]
if file:
print(file)
in request.file , I'm getting ImmutableMultiDict([])在request.file ,我得到ImmutableMultiDict([])
but in request.form["file"] , I'm getting data something like this:但是在request.form["file"] 中,我得到的数据是这样的:
how can I handle this type of file format or how can I convert this file format to python fileObject.我如何处理这种类型的文件格式,或者如何将这种文件格式转换为 python fileObject。
I solved this issue by updating my Nodejs code.我通过更新我的Nodejs代码解决了这个问题。 We need to convert formData file into octet/stream format.
我们需要将 formData 文件转换为八位字节/流格式。
so I did minor change in my formData code :所以我对我的 formData 代码做了一些小的改动:
before: formData.append("file", existingFile)
之前:
formData.append("file", existingFile)
after: formData.append("file", fs.createReadStream(existingFile)
之后:
formData.append("file", fs.createReadStream(existingFile)
Note: fs.createReadStream only accepts string or uint8array without null bytes.
注意: fs.createReadStream只接受没有空字节的字符串或 uint8array。 we cannot pass the buffer array.
我们不能传递缓冲区数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.