vue使用formdata上传多个文件

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>	
	</head>
	<body>
		<div id="app">		
		</div>
		<script src="js/vue.min.js" type="text/javascript" charset="utf-8"></script>
		<script type="text/javascript">
			const vue=Vue.extend({
     
				el:"#app",
				data:function(){
     
					return {
     
						
					}
				},
				template:`
`
, methods:{ sub(){ console.log(this.$refs.file.files) var form=new FormData(); for(let i=0;i<=this.$refs.file.files.length-1;i++){ console.log(this.$refs.file.files[i]) form.append('file'+i,this.$refs.file.files[i]); } // form.append('file',this.$refs.file.files[1]); // form.append('file',this.$refs.file.files[0]); console.log(form.get('file')) fetch('http://localhost:8000/tp5/publica/index/Api/upd', { method: 'POST', body: form }) .then(response => response.json()) .then(response => console.log('Success:', JSON.stringify(response))) } }, components:{ } }) new vue().$mount('#app') </script> </body> </html>

后端php thinkPHP
前端如果是append(‘file’,文件信息)这样就会覆盖
我只能append(‘file’+i,文件信息)PHP才能读到两个文件

如果有大佬可以使用append(‘file’,文件信息)不覆盖
实现通过form表单多选文件
然后PHP读取 一个文件数组 循环结果
请留言

public function upd(){
     
	header("Access-Control-Allow-Origin:*");
	if(!empty($_FILES)){
     
		return \json($_FILES);
	}
	else{
     
		$data['a']='无文件';
		return \json($data);
	}
}

你可能感兴趣的:(vue,vue)