问题
This code is taken from AJAX Image upload using PHP I have copied the code to these files: index.html, style.css, script.js, ajax_php_file.php and downloaded NoImage.jpg. Then I selected the image from my PC and the photo has been changed on client side to the selected image. Then I clicked upload and no file has been uploaded to the folder where php script was called. Tested in Firefox 32 Firebug console and no errors reported. I also tried this with Jquery 2.2.2 with error telling me that e.preventDefault(); is depricated. Hence can you tell me what is wrong with the code? It does not upload in Opera neither. I see there is more ppl asking this on the origin page of the script but the answer stays not responded.
I tried this code $("#uploadimage").submit();
adding after reader.readAsDataURL(this.files[0]);
and this also does not help.
Yet the live example here: http://kod.djpw.cz/vpvb
Yet a code from console. If I edit this function:
function imageIsLoaded(e) {
$("#file").css("color","green");
console.log($("#file"));
$('#image_preview').css("display", "block");
console.log($('#image_preview'));
$('#previewing').attr('src', e.target.result);
console.log($('#previewing'));
$('#previewing').attr('width', '250px');
$('#previewing').attr('height', '230px');
};
I see this:
Object { 0: <input#file>, length: 1, context: HTMLDocument → index.html, selector: "#file" } script.js:45
Object { 0: <div#image_preview>, length: 1, context: HTMLDocument → index.html, selector: "#image_preview" } script.js:47
Object { 0: <img#previewing>, length: 1, context: HTMLDocument → index.html, selector: "#previewing" } script.js:49
回答1:
I was occupied by solving the JS and did not realized the uploading must be fixed on server side by ajax_php_file.php. When I dig in the code I see line
if (file_exists("upload/" . $_FILES["file"]["name"])) {
echo $_FILES["file"]["name"] . " <span id='invalid'><b>already exists.</b></span> ";
so this means I need to create the "upload" folder in the same folder as the php script location. This was a simple fix but it was not in the original code description.
来源:https://stackoverflow.com/questions/36183350/why-jquery-image-uploader-doesnt-upload-image