0

I am trying to generate a base64 string of local URL of image. I searched a lot for that but didn't got any answer. I found many example to convert a base64 string with a HTTP URL of image but I have local URL of image. I tried it with create a file object but its not working. Here is a example what I tried:

convertFileToDataURLviaFileReader(url){
    var file = new File([""],url, {type: "image/jpeg"});
    let reader = new FileReader();
    reader.onload = (event) => {
        var base64 = event.target.result;
        return base64;
    };
    reader.readAsDataURL(file);     
}

Here URL is a absolute path of image : file:\\C:\Users\abc\Pictures\butterfly.jpg This example is not generating a base64 image completely ie only returning a blank string :

Output

data:/
9
  • You're not returning anything from your function. Commented Jun 28, 2016 at 12:08
  • forget to added a line. Question is edited now @Mardoxx Commented Jun 28, 2016 at 12:12
  • It is not working with local files, File don't have any constructor.Please see the documentation, there is no magic -> developer.mozilla.org/en-US/docs/Web/API/File , developer.mozilla.org/en-US/docs/Web/API/Blob Commented Jun 28, 2016 at 12:28
  • it will working fine when i used it with input type file. It will take a local file and i have created a base64 with input type file. But now i want to generate manually without input type file @AlexNikulin Commented Jun 28, 2016 at 12:35
  • please refer [stackoverflow.com/questions/20958078/… Commented Jun 28, 2016 at 12:48

0

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.