当前位置:网站首页>Ts+fetch to upload selected files

Ts+fetch to upload selected files

2022-06-11 00:41:00 Chenxiaohao

Upload the type :File: (binary)
Request header :content-type: multipart/form-data;

html part

<input id="updateImg" type="file"/>

js Code section

const updateImg = document.querySelector<HTMLInputElement>("#updateImg")!;
updateImg.addEventListener('change',(e:any)=>{
    
    const formData = new FormData();
    // Parameters 
     formData.append('File',e.target.files[0]);
     fetch('https://api.com/UploadImgFile', {
    
     method: 'POST',
     body: formData
     })
     .then(response => response.json())
     .then(result => {
    
     console.log('Success:', result);
     })
     .catch(error => {
    
     console.error('Error:', error);
     });
})
原网站

版权声明
本文为[Chenxiaohao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206102319527294.html