如何编码<输入文件>到 AngularJs 中的 base64? [英] How to Encode an <input file> to base64 in AngularJs?
问题描述
我需要通过 drupal 服务/文件将图像保存到 Drupal 中.据我所知,我需要将文件解码为 base64 并将其作为服务/文件的有效负载发送.我怎样才能编码才能做到这一点????我想在 javascript 中做到这一点.
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.
推荐答案
我是angular-base64-upload<的作者/a> 如@GrimurD 所述.该指令非常适合这种情况.它将图像(或任何其他文件类型)解析为 base64 编码并将文件信息附加到您范围内的模型.
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.
示例用法:
<input type='file' ng-model='yourModel' base-sixty-four-input>
一旦你选择了一个文件,yourModel
的值将类似于:
Once you selected a file, yourModel
will have a value of something like:
{
"filetype": "text/plain",
"filename": "textfile.txt",
"base64": "/asdjfo4sa]f57as]fd42sdf354asdf2as35fd4"
}
它还包含使用 PHP 或 ruby 解码服务器中 base64 文件的示例代码.
It also has sample code for decoding the base64 file in your server, using PHP or ruby.
这篇关于如何编码<输入文件>到 AngularJs 中的 base64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!