前言:
现在大家对“ajaxuploadjsapi”都比较重视,你们都需要剖析一些“ajaxuploadjsapi”的相关知识。那么小编同时在网摘上网罗了一些关于“ajaxuploadjsapi””的相关资讯,希望我们能喜欢,你们快快来学习一下吧!官方一次性上传多个文件
formData.append('title', 'My Vegas Vacation'
); for (let i = 0; i < photos.files.length; i++
) {
formData.append(`photos_${i}`, photos.files[i]);
} alert(formData)
fetch(';, { method: 'POST', body: formData,
}) .then(response => response.text())
.then(result => {
alert(result)
console.log('Success:', result);
})
.catch(error => {
console.error('Error:', error);
});
由于后台经典asp仅支持接受单文件上传,或者文件比较大,比较多,需要单个连续上传。
<html>
<body>
<div id=outTable>
</div>
<input type="file" multiple=multiple />
<input type="button" id=upload value="上传文件">
<script src=/js/onerror.js></script>
<script>
function recursivePost(url,i,arr){
let formData = new FormData();
formData.append(`photos_0`, photos.files[i]);
fetch(url,{
method: 'POST',
body: formData,
}).then(r=>r.text()).then(
(c)=>{
if(i<total){
arr[i]=c
i++
recursivePost(url,i,arr)
}else{
alert(arr.join())
outTable.innerHTML=arr.join(`%%`)}
})
}
var i,arr,total
const photos = document.querySelector('input[type="file"][multiple]');
upload.onclick=function(){
url=``
i=0
arr=[]
total=photos.files.length
recursivePost(url,i,arr)
}
</script>
</body>
</html>
后端php
<?php
$dir="upload/";
$filename=$_FILES['photos_0']["name"];
//$filename=$_FILES[0]["name"];
//print_r($_FILES);
//$keyOfFiles=array_keys($_FILES)[0];
//die($keyOfFiles);
if(move_uploaded_file($_FILES['photos_0']['tmp_name'],$dir.$filename)){
echo '{"status":"ok"'.$filename.'}';
}else{
echo '{"status":"failed"}';
}
标签: #ajaxuploadjsapi