19/09/14 10:49
giocala88
Salve ragazzi come da titolo ho un problema con l'upload del file. Posto il codice:
Form
.js
.php
In pratica succede che: il button resta fisso su Uploading... e non carica il file nella cartella upload/.
Dove sbaglio?
Grazie
Form
<form id="file-form" action="handle_file_upload.php" method="POST"> <input type="file" id="file-select" name="photos[]" multiple/> <button type="submit" id="upload-button">Upload</button> </form>
.js
var form = document.getElementById('file-form'); var fileSelect = document.getElementById('file-select'); var uploadButton = document.getElementById('upload-button'); form.onsubmit = function(event) { event.preventDefault(); // Update button text. uploadButton.innerHTML = 'Uploading...'; // The rest of the code will go here... } // Get the selected files from the input. var files = fileSelect.files; // Create a new FormData object. var formData = new FormData(); // Loop through each of the selected files. for (var i = 0; i < files.length; i++) { var file = files[i]; // Check the file type. if (!file.type.match('image.*')) { continue; } // Add the file to the request. formData.append('photos[]', file, file.name); } // Files formData.append(name, file, filename); // Blobs formData.append(name, blob, filename); // Strings formData.append(name, value); // Set up the request. var xhr = new XMLHttpRequest(); // Open the connection. xhr.open('POST', 'handle_file_upload.php', true); // Set up a handler for when the request finishes. xhr.onload = function () { if (xhr.status === 200) { // File(s) uploaded. uploadButton.innerHTML = 'Upload'; } else { alert('An error occurred!'); } }; // Send the Data. xhr.send(formData);
.php
<?php if(move_uploaded_file($_FILES['photos']["tmp_name"], "upload/".$_FILES['photos']['name'])) { echo 'Ok'; } else { echo 'NONE'; } ?>
In pratica succede che: il button resta fisso su Uploading... e non carica il file nella cartella upload/.
Dove sbaglio?
Grazie
aaa