【问题标题】:how to send multiple files from Vue js to PHP如何将多个文件从 Vue js 发送到 PHP
【发布时间】:2021-06-27 06:51:39
【问题描述】:
<form method="POST" @submit.prevent="share" >
            <div id="imgs">
            
            Select Images: <input type="file" ref="file" name="file[]" multiple ></form>

      share(){
        var formData=new FormData();
         
  for( var i = 0; i < this.$refs.file.files.length; i++ ){
        let file = this.$refs.file.files[i];
        formData.append('file[' + i + ']', file);
    };

    
        axios.post('http://localhost/11/upload_img.php',formData,{
            headers:{'Content-Type':'multipart/form-data'}
        }).then((res)=>{
            console.log("sent");
            
        });
    }}}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.4.4/vue.js"></script>
<form method="POST" @submit.prevent="share" >
                <div id="imgs">
                
                Select Images: <input type="file" ref="file" name="file[]" multiple >
        
        
        
  

我像上面一样在 vue js 中发送文件 这是我的 PHP 代码$filename=$_FILES['file']['name'];

$files=$_FILES['file'];

回声($files);

我得到了这个错误

注意:未定义索引:第 6 行 C:\xampp\htdocs\11\upload_img.php 中的文件

【问题讨论】:

    标签: php vue.js file axios multiple-file-upload


    【解决方案1】:

    您似乎没有为您的表单 enctype 属性设置“multipart/form-data”,所以您不会在后端获得 $_FILES['file']

    检查What does enctype='multipart/form-data' mean?

    【讨论】:

    • 现在我做了,但还是同样的错误,我需要改变什么吗?
    • var_dump($_FILES) 的结果是什么?
    猜你喜欢
    • 1970-01-01
    • 2017-04-26
    • 2016-07-21
    • 1970-01-01
    • 1970-01-01
    • 2019-04-11
    • 2020-05-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多