'how to send and receive file by flask and axios
In the node js, I send a post request, and hope to receive a file from the python flask server. What it sends is not relavent, and I just would like to receive the a jpeg file from the flask server:
const fs = require('fs');
const axios = require('axios');
const BASE_URL = "http://localhost:5000";
function send_test(test_name,...args) {
const apiurl = `${BASE_URL}/send`;
const form = new URLSearchParams();
form.append('filename', test_name);
const config = {
headers: {
'content-type': 'multipart/form-data'
}
};
axios.request({
url: apiurl,
method: 'POST',
headers:config,
data: form,
})
.then(function (response) {
console.log(response.data);
fs.writeFileSync('result.jpeg',
response.data);
})
.catch(function (error) {
console.log(error);
});
}
result = send_test('test_name');
The flask server returns a jpeg, and it does not matter what it receives from the node js, it does not check the form and just sends a jpeg file:
from flask import Flask, request,send_file
import os
app = Flask(__name__)
@app.route('/send', methods=['POST'])
def send():
print('send')
extracted_name = 'test.jpeg'
return send_file(extracted_name, mimetype="image/jpeg")
However, when I save file to 'result.jpeg', the file cannot be opened. I think the encoding of the file is important here. How to save the file by fs.writeFileSync()? The first a few bytes of response.data is like :
����JFIFHH��xExifII*1>i�FPicasa�0220�� ���C
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
