jQuery récupérer un fichier téléchargé avec l'entrée type = 'fichier'
J'ai envie de prendre le fichier téléchargé dans un <input type='file'>
tag.
Quand je fais $('#inputId').val(), il ne récupère que la nom du fichier et non le fichier lui-même.
Je suis en train de suivre ce:
http://hacks.mozilla.org/2011/03/the-shortest-image-uploader-ever/
function upload(file) {
//file is from a <input> tag or from Drag'n Drop
//Is the file an image?
if (!file || !file.type.match(/image.*/)) return;
//It is!
//Let's build a FormData object
var fd = new FormData();
fd.append("image", file); //Append the file
fd.append("key", "6528448c258cff474ca9701c5bab6927");
//Get your own key: http://api.imgur.com/
//Create the XHR (Cross-Domain XHR FTW!!!)
var xhr = new XMLHttpRequest();
xhr.open("POST", "http://api.imgur.com/2/upload.json"); //Boooom!
xhr.onload = function() {
//Big win!
//The URL of the image is:
JSON.parse(xhr.responseText).upload.links.imgur_page;
}
//Ok, I don't handle the errors. An exercice for the reader.
//And now, we send the formdata
xhr.send(fd);
}
source d'informationauteur Razor Storm
Vous devez vous connecter pour publier un commentaire.
Utilisation
event.target.files
pourchange
événement pour récupérer le Fichier cas.Jetez un oeil ici pour plus d'infos: http://www.html5rocks.com/en/tutorials/file/dndfiles/
Cette solution utilise le Fichier API qui n'est pas supporté par tous les navigateurs - voir http://caniuse.com/#feat=fileapi .
C'est probablement référence à HTML5
files
de la propriété. Voir w3 et de l'échantillon jsfiddle