dongqiao8417 2019-05-13 03:38
浏览 195

Cordova XMLHttpRequest PHP上传

I am working with Cordova, and am trying to upload an audio file to my server through a PHP script.

I am working off of this example

https://cordova.apache.org/blog/2017/10/18/from-filetransfer-to-xhr2.html

The example code suggests to pass the read-in file object as a blob.

reader.onloadend = function() {
// Create a blob based on the FileReader "result", which we asked to be retrieved as an ArrayBuffer
var blob = new Blob([new Uint8Array(this.result)], { type: "image/png" });
var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mysweeturl.com/upload_handler", true);
oReq.onload = function (oEvent) {
// all done!
};
// Pass the blob in to XHR's send method
oReq.send(blob);
};

I have modified the code to attach it as a data object as the file isn't being created

window.resolveLocalFileSystemURL(fileSystem, function (dir) {

dir.getFile(fileName, {create: true}, function (fileEntry) {

fileEntry.file(function (file) {

var reader = new FileReader();

reader.onloadend = function() {

var data = new FormData();

var oReq = new XMLHttpRequest();
oReq.open("POST", "http://mywebsite.com/up.php", true);

var blob = new Blob([new Uint8Array(this.result)], { type: "audio/mpeg" });
data.append('fileToUpload', blob );

oReq.onload = function (oEvent) {
// all done!

};
// Pass the blob in to XHR's send method
oReq.send(data);

};

// Read the file as an ArrayBuffer
reader.readAsArrayBuffer(file);

}

...

My php

$target_dir = $_SERVER['DOCUMENT_ROOT'] . "/uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);

if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
//echo "uploaded";
} else {
//echo "error";
}
  • I have confirmed I have read/write privileges.
  • I have confirmed I can upload the test audio file using a standard html form and the above php script.
  • I have checked my vhost error logs and Apache system logs and have not seen any errors. I can see the POST being made but no error messages.
  • I have confirmed the file object obtained by Cordova is accurate and does exist.
  • I have tried several different mime types with no effect

Any help would be much appreciated.

Thanks

  • 写回答

1条回答 默认 最新

  • dongtun4268 2019-05-21 08:52
    关注

    try this.

    <?php
       print_r($_FILES);
       $new_image_name = "YEAH.jpg";
       move_uploaded_file($_FILES["file"]["tmp_name"], "/var/www/TEST/".$new_image_name);
    ?>
    

    js code

    <script type="text/javascript" charset="utf-8">
    
    // Wait for PhoneGap to load
    document.addEventListener("deviceready", onDeviceReady, false);
    
    // PhoneGap is ready
    function onDeviceReady() {
        console.log("device ready");
        // Do cool things here...
    }
    
    function getImage() {
        // Retrieve image file location from specified source
        navigator.camera.getPicture(uploadPhoto, function(message) {
                    alert('get picture failed');
                },{
                    quality: 50,
                    destinationType: navigator.camera.DestinationType.FILE_URI,
                    sourceType: navigator.camera.PictureSourceType.PHOTOLIBRARY
                }
        );
    
    }
    
    function uploadPhoto(imageURI) {
        var options = new FileUploadOptions();
        options.fileKey="file";
        options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
        options.mimeType="image/jpeg";
    
        var params = new Object();
        params.value1 = "test";
        params.value2 = "param";
    
        options.params = params;
        options.chunkedMode = false;
    
        var ft = new FileTransfer();
        ft.upload(imageURI, "http://some.server.com/TEST/upload.php", win, fail, options);
    }
    
    function win(r) {
        console.log("Code = " + r.responseCode.toString()+"
    ");
        console.log("Response = " + r.response.toString()+"
    ");
        console.log("Sent = " + r.bytesSent.toString()+"
    ");
        alert("Code Slayer!!!");
    }
    
    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
    }
    
    </script>
    
    评论

报告相同问题?

悬赏问题

  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)