Search code examples
javascriptpythonhtmlflaskpost

How can I upload an image from JavaScript in a post request from one screen and transfer it to another screen, in Flask?


I would like to upload a picture to a screen and have that image be taken to another screen but the image is stored in a JavaScript variable. How can I take this image and send it over to another HTML screen where it can be displayed?

The simplest way to do this would be to save the image into the static folder and then loading the image from there onto the html page. But this does not seem like the most optimized way.

Is there a way to get the image directly from the post request and send it to the page from the memory?


Solution

  • After lots of searching based on several sources, I found a way to send a picture stored in JavaScript in a POST request to another screen as well as loading the picture directly from memory into another HTML page.

    First, in order to send a picture from JavaScript, I created a form with a POST method just for this image:

     <form action="/CropperScreen" method="POST" id="ImageForm" enctype="multipart/form-data">
                <input name="imageFile" id="imageFile" hidden type="file"/>
                <input hidden type="submit">
        </form>
    

    Afterwards, I get a file input responsible for sumbiting the image and store it in a variable:

    const fileInput = document.querySelector('#imageFile');
    

    I then fill the file input with the file and file information:

                // Get your file ready
                const myFileContent = [fileURL];
                const myFileName = 'test.png';
                const myFile = new File(myFileContent, myFileName);
    
                // Create a data transfer object. Similar to what you get from a `drop` event as `event.dataTransfer`
                const dataTransfer = new DataTransfer();
    
                // Add your file to the file list of the object
                dataTransfer.items.add(myFile);
    
                // Save the file list to a new variable
                const fileList = dataTransfer.files;
    
                // Set your input `files` to the file list
                fileInput.files = fileList;
    
    

    Finally, I locate the form and submit it by JavaScript code:

    document.getElementById("ImageForm").requestSubmit();
    

    Some of this work was based on: https://dev.to/code_rabbi/programmatically-setting-file-inputs-in-javascript-2p7i

    Now in order to display it onto another HTML screen, I first receive it from the routed action I specified in the form and store the image in a variable:

    @app.route('/CropperScreen',methods=['GET','POST'])
    def cropper_screen():
        if request.method == "POST":
            imageFile = request.files.get('imageFile', '')
    

    I then create an BYTESIO() object, and use the save() function in the "image" object to store the image as a byte buffer:

    imageIO = io.BytesIO()
    imageFile.save(imageIO)
    

    The buffer is converted to a byte stream, converted to ASCII and converted to base64 which is then parsed pased on the urllib library. THe resolted is added to a special uri syntax and sent to the HTML page:

    uri = 'data:image/png;base64,' + urllib.parse.quote(base64.b64encode(imageIO.getvalue()).decode('ascii'))
    
            return render_template('CropperScreen.html',uri=uri)
    

    Finally, in the HTML page, the uri syntax is used in the "src" section of an img tag like this:

    <img id="image" src="{{ uri }}">
    

    The buffer and uri idea was based on this answer: https://stackoverflow.com/a/70849754/17870878