RN:上传图片

2023-03-19  本文已影响0人  春暖花已开

用到的两个库:react-native-compressorreact-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' }
  })
上一篇下一篇

猜你喜欢

热点阅读