用到的两个库:react-native-compressor
和 react-native-image-picker
。
const {
assets: [{ uri, type, fileName }]
} = await launchImageLibrary({
mediaType: 'mixed',
presentationStyle: 'fullScreen',
quality: 0.8,
selectionLimit: 1
})
const compressedUri = await CompressImage.compress(uri, { quality: 0.8, maxWidth: 1000, maxHeight: 2000 })
console.log('assets', compressedUri)
const formData = new FormData()
// 重点在这
formData.append('file', {
// uri: 本地文件绝对路径
uri: compressedUri,
type,
name: fileName
})
const { data, error } = await uploadFile(formData)
console.log('上传图片: data, error', data, error)
// 网络请求
export const uploadFileRequest = formdata =>
request.post({
path: 'http://xxxxxxxxxx/file/v1/newUpload',
params: formdata,
header: { 'Content-Type': 'multipart/form-data' }
})
网友评论