【问题标题】:Cordova XMLHttpRequest PHP UploadCordova XMLHttpRequest PHP 上传
【发布时间】:2019-05-21 16:52:50
【问题描述】:

我正在使用 Cordova,并尝试通过 PHP 脚本将音频文​​件上传到我的服务器。

我正在处理这个例子

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

示例代码建议将读入的文件对象作为 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);
};

我已经修改了代码以将其作为数据对象附加,因为文件没有被创建

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);

}

...

我的 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";
}
  • 我已确认我具有读/写权限。
  • 我已确认我可以使用标准 html 表单和上述 php 脚本上传测试音频文件。
  • 我检查了我的 vhost 错误日志和 Apache 系统日志,没有发现任何错误。我可以看到 POST 正在进行,但没有错误消息。
  • 我已经确认 Cordova 获得的文件对象是准确的并且确实存在。
  • 我尝试了几种不同的 mime 类型,但没有任何效果

任何帮助将不胜感激。

谢谢

【问题讨论】:

    标签: php xml cordova xmlhttprequest phonegap


    【解决方案1】:

    试试这个。

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

    js代码

    <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()+"\n");
        console.log("Response = " + r.response.toString()+"\n");
        console.log("Sent = " + r.bytesSent.toString()+"\n");
        alert("Code Slayer!!!");
    }
    
    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
    }
    
    </script>
    

    【讨论】:

    • 感谢您的帖子。但是,您发布的解决方案已贬值,cordova 或 phonegap 不再支持(现在有一段时间了)。此外,该示例用于获取图像并使用 navigator.camera.getPicture 而不是通过像我提到的文件对象(如当前版本支持)
    猜你喜欢
    • 2019-04-23
    • 2011-03-03
    • 2017-05-20
    • 1970-01-01
    • 2023-04-08
    • 2017-09-30
    • 2013-06-12
    • 1970-01-01
    相关资源
    最近更新 更多