获取使用输入类型文件(多个)选择的文件,并将它们存储在数组中 [英] Get the files chosen using the input type file (multiple) and store them in an array

查看:77
本文介绍了获取使用输入类型文件(多个)选择的文件,并将它们存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的HTML中,我选择多个文件。

 < input type =fileid =imgmultiple> 

我想将每个元素存储在一个数组中,以便我可以打印文件源。 >

我已经在我的javascript中完成了这项工作,但它不起作用。

 函数loadfiles()
{
var x = document.getElementById(img);
for(var i = 0; i< x.length; i ++)
{
document.write(x.elements [i] .src);
}
}


解决方案

属性文件可以获取文件输入选择的所有文件的数组。所以loadfiles函数应该修改为:

  function loadfiles()
{
var imageFiles = document.getElementById(img),
filesLength = imageFiles.files.length;
for(var i = 0; i< filesLength; i ++){
document.write(imageFiles.files [i] .name);
}
}


In my HTML i select multiple files.

<input type="file" id="img" multiple>

I want to store each element in an array so that i can print the file source.

I have done this in my javascript but it does not work.

function loadfiles()
{
var x=document.getElementById("img");
for (var i=0;i<x.length;i++)
{
  document.write(x.elements[i].src);
}
}

解决方案

The property files gets you an array of all the files selected by the file input. So the loadfiles function should be modified to following:

function loadfiles()
{
    var imageFiles = document.getElementById("img"),
    filesLength = imageFiles.files.length;
    for (var i = 0; i < filesLength; i++) {
      document.write(imageFiles.files[i].name);
    }
}

这篇关于获取使用输入类型文件(多个)选择的文件,并将它们存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆