0

Python Flask server code receiving a XHR send POST request image and want to save it. This is the following code as I send the picture thru the web portal:

document.getElementById('file-image').src = URL.createObjectURL(file);

in which will generate the corresponding blob image link as following:

blob:http://127.0.0.1:8080/f4bbec18-31b4-4d5b-b265-409142397ac5

And my header of the request would be like following,

                xhr.open('POST', document.getElementById('file-upload-form').action, true);
                xhr.setRequestHeader('X-File-Name', file.name);
                xhr.setRequestHeader('X-File-Size', file.size);
                xhr.setRequestHeader('file-url', document.getElementById('file-image').src);
                xhr.setRequestHeader("Access-Control-Allow-Origin", "*");
                xhr.setRequestHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
                xhr.setRequestHeader('Content-Type', 'multipart/form-data');
                xhr.send(file);

Now, inside my python flask server, I want to process that "POST" request and save the image into folder:

@app.route('/cv',methods=['POST'])
def getpic():
    print(request.headers.get('file-url'))

Now I want to ask, how can I save the blob linked image and process it.

I am sure that I can see the image as I paste the link to browser as following

enter image description here

Thanks,

Toby.

3
  • Why are you sending the image in the header of your request and not using a FormData object so that it is sent in a form and is accessible as a file? Commented Sep 15, 2024 at 16:43
  • Thanks for the reply, I am thinking of to pass with the link to make the request small and neat and the server can grep the image thru the blob url link. Commented Sep 16, 2024 at 0:50
  • I don't think it's possible to exchange data directly via a blob URL. A blob URL is more of a link to a storage address of the client. The client can manipulate the data and doesn't have to retrieve it again. The raw data of your image is still the same size. Commented Sep 16, 2024 at 8:10

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.