将文件从文件输入复制/克隆到另一个输入 [英] Copy/Clone files from File Input to another Input
问题描述
如果我打开文件选择,请选择一个文件,然后再次单击文件选择,然后单击取消",它会忘记最初选择的文件
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屋!