PhoneGap的阿贾克斯上载文件 [英] PhoneGap Ajax Upload File

查看:175
本文介绍了PhoneGap的阿贾克斯上载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图运行这个的PhoneGap的例子用于从设备上传图像到服务器

I am trying to run this PhoneGap example for uploading images from the device to the server.

    // Wait for PhoneGap to load
    //
    document.addEventListener("deviceready", onDeviceReady, false);

    // PhoneGap is ready
    //
    function onDeviceReady() {

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

        var ft = new FileTransfer();
        ft.upload(imageURI, "http://some.server.com/upload.php", win, fail, options);
    }

    function win(r) {
        alert("Code = " + r.responseCode);
        alert("Response = " + r.response);
        console.log("Sent = " + r.bytesSent);
    }

    function fail(error) {
        alert("An error has occurred: Code = " + error.code);
        alert("upload error source " + error.source);
        alert("upload error target " + error.target);
    }

在我的情况下,与上面提到的例子中,我没有使用它发布的数据到数据库中的PHP文件,但使用安全的URL直接通过HTML张贴。
东西看起来在桌面版那样:

In my case, unlike the example mentioned above, I am not using a php file which posting data into the database, but using a secure url to post it directly via HTML. Something which will look on a desktop version like that:

<form action="https://mySecureUrl.com/?filename=myImage" method="post" enctype="multipart/form-data">
<input type="file" name="myfile"><br>
<input type="submit" value="Upload File to Server">
</form>

现在我想申请使用PhoneGap的文件传输(同样的方法),我得到错误code 3。

Now I am trying to apply the same method using PhoneGap FileTransfer() and I get error code 3.

var ft = new FileTransfer();
ft.upload(imageURI, encodeURI("https://mySecureUrl.com/?filename=myImage.jpg"), win, fail, options);


  • 是否有可能在PhoneGap的这种方法来发布的文件怎么样?怎么样?

  • 如果不是,究竟是什么使PHP的解决方案?

  • 推荐答案

    选项下的FileKey是服务器所要查找的名称。在HTML你有名称设置为MYFILE,并在JavaScript的你有文件。

    The fileKey under options is the name which the server is going to look for. In the HTML you have the name set to "myfile" and in the javascript you have "file".

    <input type="file" name="myfile">
    
    options.fileKey="file";
    

    options.fileKey和属性名是一样的,所以一定要确保你的PHP正在寻找的$ _FILES数组中的文件

    options.fileKey and attribute name are the same thing, so make sure your PHP is looking in the $_FILES array for "file"

    这篇关于PhoneGap的阿贾克斯上载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆