React-Native 文件上传react-native-document-picker

react-native-document-picker

下载: npm install --save react-native-document-picker

引入:import DocumentPicker from 'react-native-document-picker';

使用:

基本使用:
/**
 * res:
 * {
 *   "fileCopyUri": "content://media/external/images/media/13",
 *   "name": "IMG_20210322_071512.jpg",
 *   "size": 199984,
 *   "type": "image/jpeg",
 *   "uri": "content://media/external/images/media/13"
 * }
 */
 
// 上传单个文件
try {
  const res = await DocumentPicker.pick({
    // 可以上传的文件的类型
    type: [
      // 任何类型的文件
      DocumentPicker.types.allFiles,
      DocumentPicker.types.images,
      DocumentPicker.types.plainText,
      DocumentPicker.types.audio,
      DocumentPicker.types.pdf,
      DocumentPicker.types.zip,
      DocumentPicker.types.csv,
      DocumentPicker.types.doc,
      DocumentPicker.types.docx,
      DocumentPicker.types.ppt,
      DocumentPicker.types.pptx,
      DocumentPicker.types.xls,
      DocumentPicker.types.xlsx,
    ],
  });
} catch (err) {
  // 在文件上传过程中出现错误
  if (DocumentPicker.isCancel(err)) {
    // User cancelled the picker, exit any dialogs or menus and move on
  } else {
    throw err;
  }
}
/*————————————————————————————————————————*/
// 上传多个文件
try {
  const results = await DocumentPicker.pickMultiple({
    // 可以上传的文件的类型
    type: [
      // 任何类型的文件
      DocumentPicker.types.allFiles,
      DocumentPicker.types.images,
      DocumentPicker.types.plainText,
      DocumentPicker.types.audio,
      DocumentPicker.types.pdf,
      DocumentPicker.types.zip,
      DocumentPicker.types.csv,
      DocumentPicker.types.doc,
      DocumentPicker.types.docx,
      DocumentPicker.types.ppt,
      DocumentPicker.types.pptx,
      DocumentPicker.types.xls,
      DocumentPicker.types.xlsx,
    ],
  });

  for (const res of results) {
  }
} catch (err) {
  // 在文件上传过程中出现错误
  if (DocumentPicker.isCancel(err)) {
    // User cancelled the picker, exit any dialogs or menus and move on
  } else {
    throw err;
  }
}

案例:

上传附件:
try {
  const res = await DocumentPicker.pick({
    type: [
      DocumentPicker.types.plainText,
      DocumentPicker.types.pdf,
      DocumentPicker.types.zip,
      DocumentPicker.types.doc,
      DocumentPicker.types.docx,
      DocumentPicker.types.ppt,
      DocumentPicker.types.pptx,
    ],
  });
  Toast.info('附件上传中', 2000, 'center');
  // type在接口中有写,只有六种类型
  let type = 0;
  if (res.type === 'zip') {
    type = 5;
  }
  // res.type === 'docx' ||
  // res.type === 'doc' ||
  // res.type === 'plainText' ||
  // res.type === 'pdf' ||
  // res.type === 'ppt' ||
  // res.type === 'pptx'
  else {
    type = 6;
  }

  let formData = new FormData();
  formData.append('file', res);
  formData.append('type', type);

  let res2 = await axios.post(
    '',
    formData,
    {
      headers: {
        'Authorization': `Bearer ${this.props.RootStore.userStore.allData.accessToken}`,
        'Content-Type': 'multipart/form-data'
      },
    },
  );

  if (res2.data.code === 0) {
    Toast.success('附件上传成功', 2000, 'center');
    
  } else {
    Toast.fail(res.data.msg, 2000, 'center');
  }
} catch (err) {
  if (DocumentPicker.isCancel(err)) {
    console.log('cancleErr', err);
  } else {
    console.log(err);
    throw err;
  }
}
上传图片
try {
  const res = await DocumentPicker.pick({
    type: [DocumentPicker.types.images],
  });

  let formData = new FormData();
  formData.append('file', res);
  formData.append('type', 1);
  const ans = await axios.post('', formData, {
    headers: {
      'Authorization': `Bearer ${this.props.RootStore.userStore.allData.accessToken}`,
      'Content-Type': 'multipart/form-data',
    },
  });
  if (ans.status === 200) {
  }
} catch (err) {
  // 在文件上传过程中出现错误
  if (DocumentPicker.isCancel(err)) {
    // User cancelled the picker, exit any dialogs or menus and move on
  } else {
    throw err;
  }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值