使用POST下载rn-fetch-blob文件

时间:2019-02-06 13:09:54

标签: android react-native react-native-android

我使用的是rn-fetch-blob,如何在需要使用POST在请求正文中传递一些参数的位置下载文件?我已经尝试过了:

RNFetchBlob
            .config({
                // add this option that makes response data to be stored as a file,
                // this is much more performant.
                fileCache: true,
                path: RNFetchBlob.fs.dirs.DownloadDir + '/video.mp4'
            })
            .fetch('POST', `${SERVER}/get_video`, {
                unique_key: TerminalID(),
                id_midia: '2'
            })
            .then((res) => {
                // the temp file path
                Alert.alert('Caminho', 'The file saved to ' + res.path())
                this.setState({ download: true, downloadActionFinished: true })
            })

但是API没有收到我在请求正文中传递的数据

2 个答案:

答案 0 :(得分:0)

您基本上没有在请求中附加正文:

fetch(method, url, headers, body)

参见此处:https://github.com/joltup/rn-fetch-blob/wiki/Fetch-API

答案 1 :(得分:0)

var tempParam = [{name: 'image',filename: 'image.jpg',data: RNFetchBlob.wrap(uri}]

name是关键。如果您将文件发送到API,则需要filename。可以在fetch API中找到包装文件数据的方法(如果我没记错的话)