0
function classifyWaste(){
  var file = document.getElementById("file").files;
  if(file.length > 0){
    var form_data = new FormData();
    form_data.append("file", document.getElementById("file").files[0]);
    $.ajax({
      url: 'classifywaste', // point to server-side URL
      dataType: 'json', // what to expect back from server
      cache: false,
      contentType: false,
      processData: false,
      data: form_data,
      type: 'post',
      success: function (data) {
        var response = response["response"];

// I want to receive returned file from classifywaste() (app.py)

      document.getElementById("image_to_replace").setAttribute("src", "SOME_IMAGE_FILE");
      }

    });
  }

I found a way to do it by encoding with base64 but could not implement it clearly. So, how can I encode the image in base64 from the flask and return its string, and then decode it from the javaScript file to post in the frontend?

@application.route("/classifywaste", methods=["POST"])
def classifywaste():
    image_data = request.files["file"]
....
// Image received and performed other actions

// lets say, I have received image file after classifying 
    image_file_after = util.classify_waste(image_path)

// Here, I want to return image file to the classifyWaste() in .js file

    return jsonify(response=response)

Can you lay down the code for base64 for this case? Thanks!

VLAZ
  • 22,934
  • 9
  • 44
  • 60
Thapakaji
  • 3
  • 2

0 Answers0