Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/reactjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在react native中下载文件?_Javascript_Reactjs_React Native - Fatal编程技术网

Javascript 如何在react native中下载文件?

Javascript 如何在react native中下载文件?,javascript,reactjs,react-native,Javascript,Reactjs,React Native,我指的是,我已经转换了从API端点收到的JSON响应,现在我想将其转换为CSV文件并下载它,我该怎么做? 当用户单击下载图标时,用户必须能够将JSON响应下载为CSV文件 为了将JSON响应转换为CSV格式,我使用了以下代码来分隔每个字段。(另见截图) 代码: convertToCSV=(stm\U领导站点访问,项目名称)=>{ //日志(“在站点访问中转换CSV:”,stm_LeadSiteVisitions); 让obj=['Sr no'、'Name'、'Phone'、'Email'、'Pr

我指的是,我已经转换了从API端点收到的JSON响应,现在我想将其转换为CSV文件并下载它,我该怎么做? 当用户单击下载图标时,用户必须能够将JSON响应下载为CSV文件

为了将JSON响应转换为CSV格式,我使用了以下代码来分隔每个字段。(另见截图)

代码:

convertToCSV=(stm\U领导站点访问,项目名称)=>{
//日志(“在站点访问中转换CSV:”,stm_LeadSiteVisitions);
让obj=['Sr no'、'Name'、'Phone'、'Email'、'Project'、'visted at'、'Attended by'、'Source'、'Feedback'];
让数据=stm_LeadSiteVisitions;
设csvRow=[];

根据我的理解,对于(让item=0;item),库将文件用作缓存而不是持久存储。我曾经使用两个库从磁盘读取json文件,我想反过来(就像您的情况一样)也可以帮助您解决这个问题

下面是我用来打开带有RNFS和RNFP的文件的代码,只是给您一个起点

pickFile = () => {
    // (1) Browse for file
    DocumentPicker.show({
        filetype: [DocumentPickerUtil.allFiles()],
    }, (error, file) => {
        // (2) Copy file to accessible path
        let destPath = RNFS.DocumentDirectoryPath + '/' + file.fileName;
        RNFS.copyFile(file.uri, destPath)
            .then((success) => {
                console.log('file copied!');
                // (3) Read file content and parse to JSON
                RNFS.readFile(RNFS.DocumentDirectoryPath + '/' + file.fileName, 'utf8')
                    .then((contents) => {
                        let json = JSON.parse(contents);
                        // (4) dispatch reducer
                        this.props.addJsonFile(json);
                        ToastAndroid.show('Imported ' + file.fileName, ToastAndroid.SHORT);
                    })
                    .catch((err) => {
                        console.log('Error reading file: ' + err.message);
                    });
            })
            .catch((err) => {
                console.log('Error copying file: ' + err.message);
            });

    });
};
这是RNFS github页面中写入文件的代码:

var path = RNFS.DocumentDirectoryPath + '/test.txt';

// write the file
RNFS.writeFile(path, 'Lorem ipsum dolor sit amet', 'utf8')
  .then((success) => {
    console.log('FILE WRITTEN!');
  })
  .catch((err) => {
    console.log(err.message);
  });
刚刚在您提到的github中找到此部分:

let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
  // response data will be saved to this path if it has access right.
  path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
  //some headers ..
})
.then((res) => {
  // the path should be dirs.DocumentDir + 'path-to-file.anything'
  console.log('The file saved to ', res.path())
})
我想这并不能解决您的问题,因为保存发生在承诺解决时,您需要在保存之前将JSON转换为CSV。在这种情况下,我建议使用RNFS或向RN fetch blob添加功能请求来支持这一点

干杯

我指的是这个视频检查11分钟到11:30分钟的代码,我在做类似的事情,但在react-native中不是在react-JS中。所以我已经将数据转换为所需的格式,但现在我如何才能在react-native中下载它?在react-JS视频中,他们直接使用了'a.download()`您能在这里提供帮助吗
let dirs = RNFetchBlob.fs.dirs
RNFetchBlob
.config({
  // response data will be saved to this path if it has access right.
  path : dirs.DocumentDir + '/path-to-file.anything'
})
.fetch('GET', 'http://www.example.com/file/example.zip', {
  //some headers ..
})
.then((res) => {
  // the path should be dirs.DocumentDir + 'path-to-file.anything'
  console.log('The file saved to ', res.path())
})