1

I need to send an image from client to server using blob

I have converted a image to BLOB in jquery (client side) and sending the blob to python flask (server side) the problem is, I can't recreate image from BLOB. I have tried the following code in python but failed to get the image

Jquery code to convert image to blob:

function changeFile() {
     var file = this.files[0];
     var reader = new FileReader();
     reader.addEventListener('load', readFile);
     reader.readAsText(file);
}

function readFile(event) {
     document.body.textContent = event.target.result;
     console.log(event.target.result);
     appendFileAndSubmit(event.target.result);
}


function appendFileAndSubmit(imageUrl){
     var ImageURL = imageUrl
     var data = {
          'logo':ImageURL
          }
             $.ajax({
                        url: 'http://sdsdss/User/imgBlob',
                        method: 'POST',
                        dataType : 'json',
                        data: JSON.stringify(data),
                        contentType: 'application/json; charset=utf-8'
                    }).done(function(response){    
                        var datas = JSON.parse(response);
                        console.log(datas);
                    });
                }
document.getElementById("inp").addEventListener("change", changeFile);

Python Code: To recreate BLOB to image

function getImage(self):
     reqData = json.loads(request.data)
     Logo = reqData['logo']
     png_recovered = base64.decodestring(Logo)
     f = open("temp.png", "w")
     f.write(png_recovered)
     f.close()

1 Answer 1

1
  1. Don't read the file as text, You are dealing with binary data.
    Best way to transfer binary with json is if you read the file as base64 instead reader.readAsDataURL(file) This will encode all bytes to a web safe base64 string (no slash or plus). Then you have to decode it with python as well.

  2. I discourage you from using json when dealing with file transfer as it will increase the bandwidth with ~3 times as much (not to mention the time it also takes to decode and encode it back and forth) For this I recommend you instead use FormData.


var fd = new FormData()
fd.append('logo', files[0])

$.ajax({
  url: 'http://sdsdss/User/imgBlob',
  method: 'POST',
  data: fd,
  // Setting false prevent jQuery from transforming the data
  processData: false,
  contentType: false
}).then(function(response) {
  console.log(JSON.parse(response))
})

or simpler yet, just post the file without any formdata, json or extra fields if they are not necessary.

fetch(uploudUrl, { method 'PUT', body: this.files[0] })
Sign up to request clarification or add additional context in comments.

1 Comment

actually am sending image to api so json is the general structure am following for all the clients i can't write form data for this particular image..i will try with base64 string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.