uniapp 上传图片

374 阅读1分钟

uni.chooseImage({  
	    count: 1, // 默认9  
	    sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图,默认二者都有  
	    sourceType: ['camera','album'], // 从相册选择  
	    success: function (res) {  
	        console.log(res, 'res');  
	        let tempFilePaths = res.tempFiles; // 注意这里是 tempFiles 而不是 tempFilePaths  
	        if (tempFilePaths.length > 0) {  
	            let filePath = tempFilePaths[0].path; // 使用 path 属性  
	            console.log(filePath, 'filePath');  
	  
	            uni.uploadFile({  
	                url: `${config}/file/upload`,  
	                filePath: filePath, // 使用 filePath 而不是 files  
	                name: 'file',  
	                header: {  
	                    "jwt": jwt.value  
	                },  
	                success: (uploadFileRes) => {  
	                    console.log(uploadFileRes.data);  
	                },  
	                fail: (err) => {  
	                    console.error('Upload failed:', err);  
	                }  
	            });  
	        } else {  
	            console.log('No images selected');  
	        }  
	    }  
	});

 uni.uploadFile 会自动处理文件上传的 Content-Type 因此在请求头上不用添加Content-Type 请求头