Here i have my flask function that returns the image:
@app.route("/getResults", methods=['GET'])
def get_results():
print(request)
job_key = request.args["id"]
filename = '/home/pat/projets/giftmaker/webapp/public/workingDir/1/test.png'
return send_file(filename, mimetype='image/png')
then I have my attemp to read it:
export function Download ({jobID}) {
const [img, setImg] = useState(null);
function test() {
const params = new URLSearchParams([['id', jobID]]);
axios.get('/getResults', {params}, {responseType: "arraybuffer"})
.then((response) => {
setImg(Buffer.from(response.data, "binary").toString("base64"))
})
.catch((e) => {
console.log(e)
})
};
return(
<div>
<button onClick={test}> Display </button>
<img src={`data:image/png;base64,${img}`} />
</div>
)
};
this is the response i get from the get request :
�PNG
�
���
IHDR���h���h�����������=�zTXtRaw profile type exif��xڥ�Y��6�m��Vd�H
its actually longer than that but you get the idea…
I have tried so many thing that i found from stackoverflow but nothing seems to work.
I am testing out with a small image but it should be a gif that can be 1-5Mb
The data send from flask.send_file is binary right ?
2
Answers
I am lucky enough to control the back-end so i modified it a little bit
frontend:
Yes, the data returned from
send_file
is binary. There are multiple reasons why this could go wrong:I got it working like this:
api.py
App.js