将文件从文件输入复制/克隆到另一个输入 [英] Copy/Clone files from File Input to another Input

查看:69
本文介绍了将文件从文件输入复制/克隆到另一个输入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我打开文件选择,请选择一个文件,然后再次单击文件选择,然后单击取消",它会忘记最初选择的文件

If i open a file select, select a file, then click file select again and then click cancel, it forgets the originally selected file

//Main input
<input type="file" class="input" id="input" name="avatar">

//Backup input
<input type="file" class="input_two" id="input_two" name="avatar_two">

是否有解决方法,可能会创建一个临时输入类并将.file复制到该文件中,这样我仍然可以访问最后一个文件

Is there a workaround for this, possibly creating a temporary input class and copying the .files over to this one so that i can still have access to the last file

$('.input').change(function () {
        var value = this.files;
        console.log(value);
        if(value.length > 0)
        {
            $(.avatar_two).assign(value)... //This is what i want to do
        }
        else
        {
            console.log("nada");
        }

这可能吗?

推荐答案

无法设置< input type ="file">的 .files 属性.元素,该元素引用只读的 FileList 对象.请参见您可以调用 File.prototype.slice()创建 File 对象的副本.或使用 FormData FormData.prototype.append()存储所选文件.

You can call File.prototype.slice() to create a copy of the File object. Or use FormData, FormData.prototype.append() to store selected files.

var clone, i = 0;
var fd = new FormData();

$('.input').change(function() {
    var value = this.files;
    console.log(value);
    if (value.length > 0) {
        clone = value[0].slice(0, value[0].size, value[0].type);
        fd.append("file-" + (i++) /* this.name */, value[0]);
    } else {
        console.log("nada");
    }
});

这篇关于将文件从文件输入复制/克隆到另一个输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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