input 上傳文件type="file"

在React中使用input標(biāo)簽上傳文件

    <input onChange={ this.onChangeFiles }  type="file"  name="uploadfile" />

當(dāng)使用onChange事件時,發(fā)現(xiàn)兩次選擇同一文件會不起作用。

解決方案:
使用ref獲取當(dāng)前input,每次選擇后把valu清空
<input onChange={ this.onChangeFiles } type="file" ref={ (input) => { this.uploadInput = input; } } name="uploadfile" />

然后在onChange事件里把value置空

onChange () {
    將files對象轉(zhuǎn)化為數(shù)組
    const files = [].slice.call(e.target.files);   
    ...
    this.uploadInput = "";
}

Input上傳多個文件 multiple屬性

<input type="file" name="" multiple="" />

Input上傳文件夾 webkitdirectory屬性

<input type="file" name="" multiple="" webkitdirectory="" />
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構(gòu)建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務(wù)發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,680評論 19 139
  • Android 自定義View的各種姿勢1 Activity的顯示之ViewRootImpl詳解 Activity...
    passiontim閱讀 179,308評論 25 708
  • 原文鏈接:Styling & Customizing File Inputs the Smart Way(作者:O...
    QIQIBIBI閱讀 23,588評論 4 23
  • It's a common pattern in React to wrap a component in an ...
    jplyue閱讀 3,409評論 0 2
  • 爬過的毛毛蟲 串行的老鼠 嗡嗡不休的蚊子 靜謐中的聲響 毫無防備的嚇嚇 過了11點的夜路 半夜的驚醒 鬼故事 ……...
    三腳貓WW閱讀 491評論 0 0

友情鏈接更多精彩內(nèi)容