简体   繁体   English

base64编码的图像在“提交”按钮中不起作用

[英]base64 encoded image not working in the submit button

Here i did multiple file images with base64 conversion , everything working fine , for my requirement user select the multiple images and click the submit button means i have to take the values(file) and converted to base64 encoded string,here when select image that time it will working fine but going submit the button i am getting undefined how to solve this error 在这里,我用base64转换完成了多个文件图像,一切正常,对于我的要求用户选择了多个图像,然后单击“提交”按钮,这意味着我必须取值(文件)并转换为base64编码的字符串,在这里选择该图像时它将正常工作,但是要提交按钮,我无法定义如何解决此错误

 $(document).ready(function(){ $("input[name=property_images]").change(function() { var imgBase64Arr = []; var files = this.files; for (var i = 0; i < files.length; i++) { (function(i){ var FR = new FileReader(); FR.onload = function(e) { var res = e.target.result ; var arr1 = res.split(","); var property_image = arr1[1] ; imgBase64Arr.push( property_image);//adding base64 value to array if(i === files.length -1)//after all files are porcessed myFunction(imgBase64Arr) }; FR.readAsDataURL(files[i]); })(i); } }); }); function myFunction(imgBase64Arr){ console.log(imgBase64Arr); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <input type="file" name="property_images" multiple="multiple" /> <input type="button" value="submit" onclick="myFunction()"> 

When press submit button you call "myFunction" without passing anything.. 当按下提交按钮时,您无需传递任何内容即可调用“ myFunction”。

 $(document).ready(function(){ var imgBase64Arr = []; $("input[name=uploadBtn]").click(function(){ console.log(imgBase64Arr); }); $("input[name=property_images]").change(function() { imgBase64Arr = []; var files = this.files; for (var i = 0; i < files.length; i++) { (function(i){ var FR = new FileReader(); FR.onload = function(e) { var res = e.target.result ; var arr1 = res.split(","); var property_image = arr1[1] ; imgBase64Arr.push( property_image);//adding base64 value to array if(i === files.length -1)//after all files are porcessed myFunction(imgBase64Arr) }; FR.readAsDataURL(files[i]); })(i); } }); }); function myFunction(imgBase64Arr){ console.log(imgBase64Arr); } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> <input type="file" name="property_images" multiple="multiple" /> <input type="button" name="uploadBtn" value="submit"> 

Your imgBase64Arr variable is not globally defined. 您的imgBase64Arr变量未全局定义。 Please defined it globally. 请全局定义。

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

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