0

我必须生成一个 CSV 文件,从本地存储中提取它,将其上传到 AWS,然后将链接邮寄给用户以供访问。

iOS 正在生成 CSV、成功上传并正确发送电子邮件,而 Android 则没有。

我已经尝试了所有我能想到的组合来从本地存储/搜索多个论坛和 stackoverflow 帖子中获取文件,更改写入目录,但没有什么能接近回​​答我的问题。

使用读/写外部存储正确设置了文件权限,我有一个提示以确保用户接受访问条款,并已测试以确保目录正常工作。

对于这个特定的应用程序,我们使用的是 React Native v 0.57

代码分为两个功能:


  async function requestFileStoragePermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          'title': 'External Storage',
          'message': 'This app needs permission to save csv files to your device.'
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can save files")
      } else {
        console.log("Save files permission denied")
      }
    } catch (err) {
      console.warn(err)
    }
  }

  async function requestFileReadingPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE,
        {
          'title': 'Read csv Files',
          'message': 'This app needs permission to read csv files from your device.'
        }
      )
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        console.log("You can read files")
      } else {
        console.log("Read files permission denied")
      }
    } catch (err) {
      console.warn(err)
    }
  }

  if(Platform.OS === 'android') {
    requestFileStoragePermission()
    requestFileReadingPermission()
  }

  // write the current list of answers to a local csv file
  const pathToWrite = `${RNFetchBlob.fs.dirs.DocumentDir}/Employee_Timesheet1.csv`;


  RNFetchBlob.fs
    .writeFile(pathToWrite, csvString, 'utf8')
    .then(() => {
      console.log('Upload phase')
      fileLink = uploadCSV(pathToWrite, 'Employee_Timesheet1.csv');
        return fileLink;
    })
    .then((res) => {
      Mailer.mail({
        subject: `Timesheet for ${employeeName}`,
        recipients: ['my@email.com'],
        ccRecipients: [],
        bccRecipients: [],
        body: `<b>Employee Timesheet for ${employeeName}</b>/n<b>Click here to download ${res}</b>`,
        isHTML: true
      }, (error, event) => {
        console.log(error, event);
        Alert.alert(
          error,
          event,
          [
            { text: 'Ok', onPress: () => console.log('OK: Email Error Response') },
            { text: 'Cancel', onPress: () => console.log('CANCEL: Email Error Response') }
          ],
          { cancelable: true }
        )
      });
    })
    .catch(error => console.error(error));
}

//And then there's a function that handles uploading to AWS: 


export function uploadCSV(csvPath, fileName) {

  let type = 'text/csv'

  const fileInformation = {
    uri: csvPath,
    name: fileName,
    type: type
  }


//Options omitted due to sensitive information.

  let fileLink = RNS3.put(fileInformation, options)
    .then(response => {
      if (response.status !== 201) {
        throw new Error(response.status, response.status);
      } else {
         return response.body.postResponse.location;
      }
    })

    return fileLink;
}

当我运行以下代码时,我收到以下错误:

status: 0
text: "Could not retrieve file for uri /data/user/0/com.app/files/Employee_Timesheet1.csv"

错误消息 Android

const fileInformation = {uri : ${Platform.OS === 'android' ? 'file://' : ''}${csvPath}, name : fileName, type : type}
4

1 回答 1

0

[Content][1]应该是Arrayor String

例子

const PATH_TO_ANOTHER_FILE = "http://www.example.com/exam.jpg"
// write UTF8 data to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, 'foo', 'utf8')
              .then(()=>{ ... })
// write bytes to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, [102,111,111], 'ascii')
              .then(()=>{ ... })
// write base64 data to file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, RNFetchBlob.base64.encode('foo'), 'base64')
              .then(()=>{ ... })
// write file using content of another file
RNFetchBlob.fs.writeFile(PATH_TO_WRITE, PATH_TO_ANOTHER_FILE, 'uri')
              .then(()=>{ ... })

于 2019-08-14T05:07:36.990 回答