javascript - 如何在 AngularJs 中将 <input file> 编码为 base64?

标签 javascript drupal

我需要通过 drupal 服务/文件将图像保存到 Drupal 中。据我了解,我需要将文件解码为 base64,并将其作为服务/文件的有效负载发送。我怎样才能编码才能做到这一点????我想在 javascript 中执行此操作。

最佳答案

我是 angular-base64-upload 的作者正如@GrimurD 所提到的。该指令非常适合这种情况。它将图像(或任何其他文件类型)解析为 base64 编码并将文件信息附加到您范围内的模型。

示例用法:

<input type='file' ng-model='yourModel' base-sixty-four-input>

一旦您选择了一个文件,yourModel 的值将类似于:

{
  "filetype": "text/plain",
  "filename": "textfile.txt",
  "base64": "/asdjfo4sa]f57as]fd42sdf354asdf2as35fd4"
}

它还有使用 PHP 或 ruby​​ 解码服务器中 base64 文件的示例代码。

关于javascript - 如何在 AngularJs 中将 &lt;input file> 编码为 base64?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17549968/

相关文章:

javascript - 如果没有特定类(class)的 child ,则删除 parent

javascript - 谷歌云函数: required/exported functions throw "handler is not a function" error

javascript变量作为获取参数

php - 如何在 drupal 7 中获取某个父项下的所有菜单项?

drupal mailchimp 事件内容部分缺失

javascript - 在 MaterialUI 和 React.js 中为 Autocomplete 分配默认值

Javascript 无法更改 css 文件中的样式?

php - 此用例的 View 或有机组

html - 如何从 drupal 模块输出 HTML 的好方法

iframe - ckeditor 在编辑器中添加 &lt;iframe&gt; 标签