Solution
function downloadFile(src, dst) {
axios
.get(src, { responseType: 'stream' })
.then(async resp => {
resp.data.pipe(fs.createWriteStream(dst));
});
}
downloadFile(
'http://localhost:5000/download/test.jpeg',
'result.jpeg'
);
@app.route('/download/')
def download(filename):
return send_from_directory(app.static_folder, filename)
function downloadFile(src, dst) {
const url = 'http://localhost:5000/send';
const formData = new FormData();
formData.append('filename', src);
axios
.post(url, formData, { headers: formData.getHeaders(), responseType: 'stream' })
.then(async resp => {
resp.data.pipe(fs.createWriteStream(dst));
});
}
downloadFile(
'test.jpeg',
'result.jpeg'
);
const url = 'http://localhost:5000/upload';
const filePath = path.resolve('result.jpeg');
fs.readFile(filePath, async (error, data) => {
if (error) {
console.log(error);
return;
}
const formData = new FormData();
formData.append('file', data, { filepath: filePath, contentType: 'image/jpg' });
axios
.post(url, formData, { headers: formData.getHeaders() })
.then(resp => {
console.log('File uploaded successfully.');
});
});
@app.route('/upload', methods=['POST'])
def upload():
file = request.files.get('file');
# Process the file here.
return '', 200
from flask import send_file
import json
@app.route('/send')
def send_json_binary():
filename = 'test.jpeg'
resp = send_file(filename, mimetype='image/jpeg')
resp.headers['X-Metadata'] = json.dumps({
'filemeta': 'Your metadata here.'
})
return resp
function downloadFile(src, dst) {
axios
.get(src, { responseType: 'stream' })
.then(async resp => {
const meta = JSON.parse(resp.headers['x-metadata'])
const filemeta = meta.filemeta;
resp.data.pipe(fs.createWriteStream(dst));
});
}
downloadFile(
'http://localhost:5000/send',
'result.jpeg'
);
from flask import jsonify
from base64 import b64encode
@app.route('/sample')
def send_json_binary():
filename = 'test.jpeg'
with open(filename, 'rb') as fh:
return jsonify(
filemeta = 'Your metadata here.',
filedata = b64encode(fh.read()).decode()
)
function downloadFile(src, dst) {
axios
.get(src, { responseType: 'json' })
.then(async resp => {
const meta = resp.data.filemeta;
const buffer = Buffer.from(resp.data.filedata, 'base64');
const stream = Readable.from(buffer);
stream.pipe(fs.createWriteStream(dst))
});
}
downloadFile(
'http://localhost:5000/send',
'result.jpeg'
);