doushua7737
2018-10-23 17:30
浏览 49
已采纳

nativescript-background-http上传文件到php

I want to upload audio file on my server using nativescript-background-http plugin. The mobile side is working fine. no runtime error. The log shows complete upload but when I visit the directory on my server, no file is uploaded.

Here's my code on nativescript

var session = bghttp.session("image-upload");
var audioFolder = fs.knownFolders.documents().getFolder("audio");
var recordedFile = audioFolder.getFile("recording.mp3");
var request = {
    url: "http://talktext.me/talktext.me(app)/recAudio.php",
    method: "POST",
    headers: {
        "Content-Type": "application/octet-stream",
        "File-Name": "recording.mp3"
    },
    description: "{ 'uploading': '" + "recording.mp3" + "' }"
};
alert(recordedFile.path);
let params = [
            { name: "test", value: recordedFile.path },
            { name: "fileToUpload", filename: recordedFile.path, mimeType: 'audio/mpeg' }
];
var task = session.multipartUpload(params, request);

task.on("progress", logEvent);
task.on("error", logEvent);
task.on("complete", logEvent);
function logEvent(e) {
   console.log("UPLOAD STATUS: "+e.eventName);

}

Here's my code on recAudio.php

$all_headers = apache_request_headers();
$newimg = file_put_contents('uploads/' . $all_headers['File-Name'], 
file_get_contents('php://input'));

Everything on mobile is working fine. But no file is uploaded on the server.

图片转代码服务由CSDN问答提供 功能建议

我想使用nativescript-background-http插件在我的服务器上传音频文件。 移动端工作正常。 没有运行时错误。 日志显示完整上载,但是当我访问服务器上的目录时,没有上载文件。

这是我在nativescript上的代码

  var session = bghttp.session(“image-upload”); 
var audioFolder = fs。  knownFolders.documents()。getFolder(“audio”); 
var recordedFile = audioFolder.getFile(“recording.mp3”); 
var request = {
 url:“http://talktext.me/talktext.me(  app)/recAudio.php“,
方法:”POST“,
标题:{
”Content-Type“:”application / octet-stream“,
”文件名“:”recording.mp3“  
},
 description:“{'uploaded':'”+“recording.mp3”+“'}”
}; 
alert(recordedFile.path); 
 
 
句间= [
 {名称:“  test“,value:recordedFile.path},
 {name:”fileToUpload“,filename:recordedFile.path,mimeType:'audio / mpeg'} 
]; 
var task = session.multipartUpload(params,request);  
 
task.on(“progress”,logEvent); 
task.on(“error”,logEvent); 
task.on(“complete”,logEvent); 
function logEvent(e){
 console.log  (“UPLOAD STATUS:”+ e.eventName); 
 
} 
   
 
 

这是我在recAudio.php上的代码 < 预> $ all_headers = apache_request_headers(); $ newimg = file_put_contents('uploads /'。 $ all_headers ['File-Name'], file_get_contents('php:// input'));

移动设备上的所有内容都运行正常。 但是没有文件上传到服务器上。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • dpp89959 2018-10-23 18:22
    最佳回答

    I'm not a PHP expert but I used to work on it a long time ago. Looks like php://input is not available for multipart uploads.

    Source: http://php.net/manual/en/wrappers.php.php

    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题