Here is the html element we are trying to attach a file to:
<input id="uploaded_file_file_for_upload" class="file optional" type="file" name="uploaded_file[file_for_upload]">
User already selects a file for the element above. What we need to do is to remove the current file selection and attach a new file to it. The new file is an image file and is saved in a canvas
element. Here is the js code for new file:
function resize(image, width, height) {
var mainCanvas = document.createElement("canvas");
mainCanvas.width = width;
mainCanvas.height = height;
var ctx = mainCanvas.getContext("2d");
alert(width + ' resize to ' + height);
ctx.drawImage(image, 0, 0, width, height);
.....(remove current and attach the new file to the elelment#uploaded_file_file_for_upload)
};
We tried the following to attach and it did not work:
$('#uploaded_file_file_for_upload').attr('src', mainCanvas.toDataURL("image/jpeg"));
The problem may be that it is not an image element. What's the right way to remove and attach a file type?