1

I have an event listener set up in jQuery $('#file').on("change", function() {});.

This detects when image has been appended.

Image source is: data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ... the plugin does this.

I just want to send that created image to file upload input in my html form. How can I do this?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
LazyPeon
  • 339
  • 1
  • 19

2 Answers2

1

Try something like this then:

<script type="text/javascript">
$(function(){
   $("plugin").change(function(e){
      $("input#image").val(img);
   });
})
</script>
<form>
  <input type="hidden" name="image" id="image" />
  <input type="submit" />
</form>
<?php
if(getenv("REQUEST_METHOD") === "POST"){
   $image = $_POST["image"];
   // do what you want
}
?>

The more information you give, the better we can help you. Also, it's worth trying things yourself as you will learn from mistakes you make.

  • `SecurityError: The operation is insecure.` reads my consoel log – LazyPeon Oct 03 '14 at 13:36
  • 1
    You've got 3 options: 1. make a jsfiddle with your code. 2. try to debug it yourself. 3. provide more information about your code and goal –  Oct 03 '14 at 13:38
  • We were on the right track here but it seems that the newly created base64 string cant be just inserted into form field, I get a security warning. I am using Codeigniter though, maybe this is the problem – LazyPeon Oct 03 '14 at 13:40
0

Why don't you send it as an hidden input field? After you post the form, you can do whatever you like with it using php. Something like this:

<script type="text/javascript">
$(function(){
   $("plugin").change(function(e){
      $("input#image").val(e.image);
   });
})
</script>
<form>
  <input type="hidden" name="image" id="image" />
  <input type="submit" />
</form>
<?php
if(getenv("REQUEST_METHOD") === "POST"){
   $image = $_POST["image"];
   // do what you want
}
?>

Haven't tested this, but it should be enough to help you get going. If you want to display the image, just do something like this:

<img class="thumbnail" src="data:image/gif;base64,R0lGODlh9AEZAdUyAK6VZqBmUNjRrNSon+7">
  • `$("input#image").val(e.image);` will get my appended image data and send it to hidden field? – LazyPeon Oct 03 '14 at 13:19
  • It's worth noting that a hidden input field is inherently not secure, and if you are going to use this to get a file to upload, malicious users may be able to splice their own file into header information and force an upload to your server. – Mark Oct 03 '14 at 13:19
  • @LazyPeon As you can read in jquery documentation: http://api.jquery.com/val/ it will indeed change the value of your input field. –  Oct 03 '14 at 13:22
  • but how can I get my appendet image which source looks like: `data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...` – LazyPeon Oct 03 '14 at 13:25
  • What do you mean? `data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...` will go to the input field as it's value. –  Oct 03 '14 at 13:27
  • I get my img like so: `var img = cropper.getAvatar()` which looks like `img = data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQ...` how can I get this data to my form field? – LazyPeon Oct 03 '14 at 13:32