Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 重置用于文件上载的数组_Javascript_Php_Laravel_Vue.js - Fatal编程技术网

Javascript 重置用于文件上载的数组

Javascript 重置用于文件上载的数组,javascript,php,laravel,vue.js,Javascript,Php,Laravel,Vue.js,附件将继续选择上一个文件。如果我选择了3个文件,然后再次选择2个文件,则总文件数将变为5个而不是2个。附件在重新选择之前一直添加文件,而在我要重新选择时不会重置。提前谢谢 模板 <input id="upload-file" type="file" multiple class="form-control" @change="fieldChange"> 脚本 <script> export default { data(){ return{

附件将继续选择上一个文件。如果我选择了3个文件,然后再次选择2个文件,则总文件数将变为5个而不是2个。附件在重新选择之前一直添加文件,而在我要重新选择时不会重置。提前谢谢

模板

<input id="upload-file" type="file" multiple class="form-control" @change="fieldChange">

脚本

<script>
export default {
    data(){
        return{
          year:'',
          attachments:[],
          form: new FormData
        }
    },
    methods:{
        fieldChange(e){
            let selectedFiles=e.target.files;
            if(!selectedFiles.length){
                return false;
            }
            for(let i=0;i<selectedFiles.length;i++){
                this.attachments.push(selectedFiles[i]);
            }
            console.log(this.attachments);
        },
   }
</script>
 uploadFile(){
            for(let i=0; i<this.attachments.length;i++){
                this.form.append('pics[]',this.attachments[i]);
            }
            this.form.append('year',this.year);
            axios.post('/api/gallery',this.form).then(response=>{
                console.log(response);
                this.form.pics = [];
            })
            .catch(response=>{
                //error
            });
        }

导出默认值{
数据(){
返回{
年份:'',
附件:[],
表格:新表格数据
}
},
方法:{
现场更改(e){
让selectedFiles=e.target.files;
如果(!selectedFiles.length){
返回false;
}

for(设i=0;i,因为您正在推送附件,但未重置它

试试这个


导出默认值{
数据(){
返回{
年份:'',
附件:[],
表格:新表格数据
}
},
方法:{
现场更改(e){
this.attachments=[];//添加了这个
让selectedFiles=e.target.files;
如果(!selectedFiles.length){
返回false;
}

对于(让我=0;我工作得很好,介意看看我的新问题。我编辑了这篇文章。谢谢这个.form.pics不会工作,因为这个.form不是对象。建议不要在数据中声明
new FormData
。尝试在
字段更改中声明
const form=new FormData
methods@HelloWorld1014如果这个答案有帮助的话呃,接受这个作为解决办法,谢谢!!