0

In a textarea, people can paste pictures. I handle that with AngularJS, the ng-paste directive and this piece of code

ctrl.handlePaste = function(event) {
    if(event.clipboardData.items.length > 0) {
        for(var i = 0; i < event.clipboardData.items.length; i++) {
            var item = event.clipboardData.items[i]; // type: DataTransferItem
            if (item.type.indexOf("image") != -1) {
                file = item.getAsFile(); // type: File
                // it's then stored in a array and will be sent to the server
                break;
            }
        }
    }
}

Then I send it to my Flask server (Python 2.7) and it is then sent by emails or pushed via API to another service.

On server side I've checked the type and it's a Flask FileStorage.

My problem is: this file is automatically named "image.png" (I'm using Chrome) and I cant find a way to change this name during the process.

I'm ok with changing it on front side (my favourite option), I'm ok on server side too.

Valentin Coudert
  • 1,759
  • 3
  • 19
  • 44
  • What type of object is returned by `item.getAsFile()`? In what form is it sent to backend? base64 or binary? There is not enough infromation here to identify an adequate answer. – georgeawg Apr 19 '19 at 15:41

1 Answers1

0

If the code uses the formData API, the filename is the third (optional) argument to the formdata.append method. If the code sends the image as a blob with XHR send the filename can be sent in a query param or a Content-Disposition header.

georgeawg
  • 48,608
  • 13
  • 72
  • 95