美文网首页
RN:上传图片

RN:上传图片

作者: 春暖花已开 | 来源:发表于2023-03-19 18:03 被阅读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' }
      })
    

    相关文章

      网友评论

          本文标题:RN:上传图片

          本文链接:https://www.haomeiwen.com/subject/kluyrdtx.html