简体   繁体   English

在javascript中以base64字符串转换图像?

[英]Converting the image in base64 string in javascript?

i have html form with ENCTYPE="multipart/form-data" .It just contains the textbox of type 'file' which browses the file from local box.Here is the textbox code snippet 我有带有ENCTYPE =“ multipart / form-data”的html表单。它仅包含类型为'file'的文本框,该文本框从本地框浏览文件。这里是文本框代码段

<td>
    <input name="fileNameAttr" id="filePath" size="52" type="file" value="">
</td>

I browse the file some image file from local box. 我从本地框中浏览了一些图像文件。 The moment i select the file, filePath gets populated in text box. 我选择文件的那一刻,filePath就会填充在文本框中。 Now on click of some button, i want to convert the image in to base64 String in javascript. 现在单击某些按钮,我想将图像转换为javascript中的base64字符串。 I am not using HTML5. 我没有使用HTML5。 Can i achieve it in javascript? 我可以用JavaScript实现吗?

i can see some solution on net using HTML5 but that does not solve my purpose. 我可以在网上看到一些使用HTML5的解决方案,但这不能解决我的目的。 If i need to use some third party utility , i can do that. 如果我需要使用某些第三方实用程序,则可以这样做。

EDIT:- looks like jquery can help with $.base64.encode( "this is a test" ); 编辑:-看起来jquery可以帮助$ .base64.encode(“ this is a test”); but how to use it with type="file"? 但是如何与type =“ file”一起使用?

this can be done by the HTML5 <canvas> for it 这可以通过HTML5 <canvas>完成

or 要么

if you have node.js as an option then try something like that in img 如果您将node.js作为选项,则尝试在img中进行类似的操作

or <img src="data:image/png;base64,jkasfhsdjkhsadf" /> <img src="data:image/png;base64,jkasfhsdjkhsadf" />

example

var fs = require('fs');
var base64_data = new Buffer(fs.readFileSync('sample.png')).toString('base64');
console.log('<img alt="sample" src="data:image/png;base64,' + base64_data + '">');

check this (also source of example) 对此进行检查(也是示例来源)

Data URI scheme 数据URI方案

Good read 好读

  1. How can you encode to Base64 using Javascript? 如何使用Javascript编码为Base64? //its for image //用于图像

If you could access the local filesystem via javascript, what would keep you from reading the password file or other private data? 如果您可以通过javascript访问本地文件系统,那么什么会使您无法读取密码文件或其他私有数据?

This you can't do and it can't and won't be allowed in the future, for obvious reasons! 出于明显的原因,您不能这样做,将来也不会这样做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM