javascript - 如何将音频片段从react-native上传到php后端?

标签 javascript php codeigniter reactjs react-native

尝试了许多第三方库,例如 axios、fetch、fetch-blob,但没有成功。 这段代码包括我的 php 后端。 使用 rn v 0.39

将手机存储中已存在的音频片段上传到服务器

文件路径:AudioUtils.MusicDirectoryPath + '/test.aac' [可以访问该路径,可以播放剪辑]

使用引用:/github.com/g6ling/react-native-uploader

Server response

“无法检索 uri/storage/emulated/0/Music/test.aac 的文件”

权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />                                                    
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

React-native 代码

const form= new FormData();

form.append('userfile', {
    uri:  AudioUtils.MusicDirectoryPath + '/test.aac',
    type: 'audio/aac', 
    name: 'userfile'
});

  let xhr = new XMLHttpRequest()
  xhr.open('post', `http://vidyarangam.com/next/upload_file`)
  xhr.send(form)
  xhr.onerror = function(e) {
  console.log('err', e)
 }
 xhr.onreadystatechange = function() {
 if(this.readyState === this.DONE) {
  console.log(this.response)
 }
}

php代码

function upload_file() {
    $droidinput = json_decode(file_get_contents('php://input'), true);
    $file = $droidinput['userfile']; // i have no idea how to upload it   
    $config['upload_path'] = 'uploads/';    
    $config['allowed_types'] = '*';  
    $config['max_filename'] = '255';  
    $config['encrypt_name'] = TRUE;   
    $config['max_size'] = '1024'; //1 MB
    $this->load->library('upload', $config);

    if (!$this->upload->do_upload('userfile')) {
        echo json_encode('status'=>'File upload error');       
    } else {
        echo json_encode('status'=>'File successfully uploaded');
    }          
}

最佳答案

修复

使用的库-react-native-fetch-blob

代码

    import RNFetchBlob from 'react-native-fetch-blob';

    let dirs = RNFetchBlob.fs.dirs;

    let path_to_a_file = dirs.DownloadDir + '/header_logo.png';

  RNFetchBlob.fetch('POST', 'http://192.168.43.236/upload.php', {
    Authorization : "Bearer access-token",
    otherHeader : "foo",
    'Content-Type' : 'multipart/form-data',
  }, [

    { name : 'header_logo', filename : 'header_logo.png', type:'image/foo', data:RNFetchBlob.wrap(path_to_a_file)},
  ]).then((resp) => {

    console.log(resp.text())

  }).catch((err) => {

    console.log(err)

  })

关于javascript - 如何将音频片段从react-native上传到php后端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41585729/

相关文章:

javascript - React Native : . map() 函数在渲染中不显示任何内容

PHP Ratchet NGINX 失败 WebSocket 连接握手错误响应代码 : 502

PHP 错误 'Array' 中的未知列 'field list' 和警告 : mysql_query() expects parameter 1 to be string?

php - 在长时间的过程中连接丢失以在数据库 sqlite3 codeigniter 中保存记录

javascript - 如何利用 JavaScript 和 ES5 从基于关键字段的对象数组中获取唯一值

javascript - 如何在 Node 中显示json格式的行内容?

javascript - 如何使用 PHP 将信息从 Javascript 获取到 MySQL 数据库?

PHP 函数返回不起作用

javascript - 如何在codeigniter中使用引导弹出窗口

php - CodeIgniter 项目的第一个 HTTP 请求延迟非常高