2017-02-28 40 views
0

如果我打開一個文件選擇,選擇一個文件,然後單擊文件再次選擇,然後單擊取消,它忘記了最初選定的文件複製/克隆到另一個文件輸入

//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"> 

是否有解決方法對於這一點,有可能創建一個臨時輸入類,並在複製.files這一個,這樣我仍然可以訪問的最後一個文件

$('.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"); 
     } 

這可能嗎?

+0

不能設置的文件輸入 – epascarello

+0

函數副本中的值() { 變種myObject的,F; myObject = new ActiveXObject(「Scripting.FileSystemObject」); f = myObject.file.copy(「c:\\ test.txt」,「c:\\ mytest.txt」); } – Sarma

+0

ActiveX?是90年代嗎? – epascarello

回答

4

不可能設置 .files屬性 <input type="file">元素,其中引用只讀 FileList對象。 參見React/Javascript--FileReader/<input/>/adding image。您可以撥打File.prototype.slice()創建File對象的副本。或使用FormData,FormData.prototype.append()來存儲選定的文件。

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"); 
    } 
}); 
相關問題