当前位置:首页 > Web开发 > 正文

原生js上传图片遇到的坑

2024-03-31 Web开发

/*原生请求方式 */ let xhr = new XMLHttpRequest(); // XMLHttpRequest 对象 xhr.open("post", "http://localhost:8082/api/files/api/upload/uploadFile", true); //post方式,url为服务器请求地址,,true 该参数规定请求是否异步处理。 xhr.setRequestHeader(‘content-type‘, ‘multipart/form-data‘)//不要自作多情加上这一句 //xhr.setRequestHeader(‘Accept‘, ‘text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8‘) xhr.send(formData); //开始上传,发送form数据 xhr.onreadystatechange = function () { var data = xhr.responseText; console.log(data); }

温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/43079.html