How can I upload an image from JavaScript in a post request from one screen to another screen, in Flask? (Preferably without storing the image)

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?

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(file);

            // 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:
/a/70849754/17870878

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật