JavaScript多文件上传及图片预览

  • 多文件上传:

    var xhr = new XMLHttpRequest,
        fd = new FormData,
        i = 0;
    //files: $('input[type=file]')[0].files
    for (; i < files.length; i++) {
        fd.append('files[]', files[i]);
    }
    xhr.open('POST', '···.php');
    xhr.onreadystatechange = function() {
        if (this.status == 200 && this.readyState == 4) {
            console.log(this.responseText)
        }
    }
    xhr.send(fd);
    
  • 上传图片预览:

    //file: $('input[type=file]')
    file.on('change', function() {
        var reader = new FileReader;
        reader.onload = function(e) {
            img.src = this.result;
        }
        reader.readAsDataURL(this.files[0]);
    })
    
  • 相关链接:

    • FileReader
    • FileList
    • File

你可能感兴趣的:(JavaScript多文件上传及图片预览)