当前位置:网站首页>ts+fetch实现选择文件上传

ts+fetch实现选择文件上传

2022-06-10 23:20:00 陈小浩同学

上传类型:File: (binary)
请求头:content-type: multipart/form-data;

html部分

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

js代码部分

const updateImg = document.querySelector<HTMLInputElement>("#updateImg")!;
updateImg.addEventListener('change',(e:any)=>{
    
    const formData = new FormData();
    //参数
     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);
     });
})
原网站

版权声明
本文为[陈小浩同学]所创,转载请带上原文链接,感谢
https://blog.csdn.net/xiaodouxuan11/article/details/124989280