Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/454.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在javascript中获取所选文件列表?_Javascript - Fatal编程技术网

如何在javascript中获取所选文件列表?

如何在javascript中获取所选文件列表?,javascript,Javascript,我正试图在HTML5的帮助下创建一个带有进度条的文件上传。正在尝试创建多个文件上载。下面是我的代码。在handleFiles函数中,我有一个alert语句,它返回“undefined”。有人能帮我吗 $('#photoimg').change(function() { handleFiles(this.files); alert('got it'); var $fileUpload = $("#photoimg"); if (parseInt($fileUpload.g

我正试图在HTML5的帮助下创建一个带有进度条的文件上传。正在尝试创建多个文件上载。下面是我的代码。在handleFiles函数中,我有一个alert语句,它返回“undefined”。有人能帮我吗

$('#photoimg').change(function()            
{ 
handleFiles(this.files); alert('got it');
var $fileUpload = $("#photoimg");
if (parseInt($fileUpload.get(0).files.length)>$('#galCnt').val())
{
    alert("Maximum of 5 images allowed to upload. Only "+$('#galCnt').val()+" left"); 
    return false;
} 

//var formData = new FormData($('#imageform')[0]);
fd = collectFormData();
// Attach the files.
for (var i = 0, ie = PENDING_FILES.length; i < ie; i++) {
    // Collect the other form data.
    fd.append("file", PENDING_FILES[i]);
}

// Inform the back-end that we're doing this over ajax.
fd.append("__ajax", "true");

$("#imageloadstatus").show();
//$("#imageloadbutton").hide();
var $progressBar = $("#progress-bar");
$progressBar.css({"width": "0%"});

var xhr = $.ajax({
    xhr:function() {
        var xhrobj = $.ajaxSettings.xhr();
        if (xhrobj.upload) {
            xhrobj.upload.addEventListener("progress", function(event) {
                var percent = 0;
                var position = event.loaded || event.position;
                var total    = event.total;
                if (event.lengthComputable) {
                    percent = Math.ceil(position / total * 100);
                }

                // Set the progress bar.
                $progressBar.css({"width": percent + "%"});
                $progressBar.text(percent + "%");
                $("#loaded_n_total").innerHTML = "Uploaded "+event.loaded+" bytes of "+event.total;
            }, false)
        }
        return xhrobj;
    },
    url: SITE_URL+'/index/ajax',
    type: 'POST',
    data: formData,
    async: false,
    cache: false,
    contentType: false,
    processData: false,
    success: function (returndata) {
      $progressBar.css({"width": "100%"});
      $("#imageloadstatus").hide();
      $('#preview').html(returndata);
    }
  });
});


var PENDING_FILES = [];

function collectFormData() {
// Go through all the form fields and collect their names/values.
var fd = new FormData();

$("#imageform :input").each(function() {
    var $this = $(this);
    var name  = $this.attr("name");
    var type  = $this.attr("type") || "";
    var value = $this.val();

    // No name = no care.
    if (name === undefined) {
        return;
    }

    // Skip the file upload box for now.
    if (type === "file") {
        return;
    }

    // Checkboxes? Only add their value if they're checked.
    if (type === "checkbox" || type === "radio") {
        if (!$this.is(":checked")) {
            return;
        }
    }

    fd.append(name, value);
});

return fd;
}


function handleFiles(files) {
    // Add them to the pending files list.
    for (var i = 1; i <= files.length; i++) 
    {
       alert(files[i]);
       PENDING_FILES.push(files[i]);
    }
}
$('#photoimg')。更改(函数()
{ 
handleFiles(this.files);警报('got it');
var$fileUpload=$(“#photoimg”);
if(parseInt($fileUpload.get(0.files.length)>$('#galCnt').val())
{
警报(“最多允许上载5个图像。仅“+$('#galCnt').val()+”左”);
返回false;
} 
//var formData=new formData($('#imageform')[0]);
fd=collectFormData();
//附上文件。
对于(var i=0,ie=PENDING_FILES.length;i对于(var i=1;i使用id
#photoimg
输入具有多个属性集,为什么
i
handleFiles()中的循环中设置为1而不是0
是的。代码运行良好。为了添加progressbar,我做了一些更改,现在我使用了HandleFiles函数。在您的
函数HandleFiles
中,它不应该从循环中的0开始吗?@R Lam,是的..这是正确的:(.我错过了它。谢谢。我总是犯这种简单的错误,并且花费了很多时间..):(id为
#photoimg
的输入是否设置了多个属性,为什么
i
handleFiles()的循环中设置为1而不是0)
是的。代码运行良好。为了添加progressbar,我做了一些更改,现在我使用了HandleFiles函数。在您的
函数HandleFiles
中,它不应该从循环中的0开始吗?@R Lam,是的..这是正确的:(.我错过了它。谢谢。我总是犯这种简单的错误,并且花费了很多时间..):(