I need to save an image into Drupal, through the drupal service/file. For what i understand i need to decode the file into a base64 and send this as a payload for the service/file. How can i get to encode the in order to do this???? I want to do this in javascript.
相关问题
- Is there a limit to how many levels you can nest i
- How to toggle on Order in ReactJS
- void before promise syntax
- Keeping track of variable instances
- Can php detect if javascript is on or not?
I was having the same problem and ran into this repository on github. Tried it out and it worked perfectly.
I am the author of angular-base64-upload as mentioned by @GrimurD. This directive works perfectly for this scenario. It parses image(or any other file types) into base64-encoding and attach the file info to a model in your scope.
Sample usage:
Once you selected a file,
yourModel
will have a value of something like:It also has sample code for decoding the base64 file in your server, using PHP or ruby.
i found some websites,browser like chrome,ie10..may be support this https://developer.mozilla.org/en-US/docs/Web/API/FileReader?redirectlocale=en-US&redirectslug=DOM%2FFileReader
http://www.pjhome.net/web/html5/encodeDataUrl.htm
I would recommend you to use https://github.com/ninjatronic/angular-base64.
After following instructions for using this library, you can simply call:
Don't forget to inject in your module:
I know it's quite old, but I came here for a solution.
In the end I found out that (if you don't want to use external libraries) loading an image in base 64 is as simple as using javascript FileReader.readAsDataURL()
Hopefully my final code will help others beginners like me. It features:
input type='file'
(inspired by this answer)input type='file'
(inspired by this answer)It is also on codepen
You don't need AngularJs for that. There is a thread here explaining how to do it using Javascript and canvas: How to convert image into base64 string using javascript