0

how can I pass data, actually file's base64 code, straight from flask(python) to javascript, but without using render_template or redirect?

I need to do that, because send_from_directory is downloading .pdf file straight to user's computer, I need just a pre-view of .pdf file, so I get file's code by .read(), encode with base64, and what I want to do is decode that in js/jquery and generate new pdf with that code.

1
  • Why don't you consider passing the pdf file in base64 encoded format through a json string? And why is that you do not want to use render_template. Anyways it is not going to transmit the file to a user's machine if you use it rightly Feb 24, 2020 at 11:43

1 Answer 1

0

You can send the data to your javascript app as JSON from Flask:

from flask import Flask, jsonify

@app.route(..)
def fn_name():
    # logic here
    # pdf_base64 is variable containing data
    return jsonify(result=pdf_base64)
1
  • Happy to help, and welcome to Stack Overflow. If this answer or any other one solved your issue, please consider marking it as accepted.
    – enigma
    Feb 25, 2020 at 4:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.