简体   繁体   English

Cordova通过文件传输上传多个图像

[英]Cordova multiple image upload with File Transfer

I'm on a cordova and jquery mobile project. 我正在使用cordova和jquery移动项目。

I've been able to upload one image with file transfer plugin. 我已经能够使用文件传输插件上传一个图像。 Now i try to upload 2 or 3 images following. 现在我尝试上传2或3张图片。

here is the html code: 这是html代码:

<label for="image">Pictures:</label>
<a href="" id="image1Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);">Get first picture</a><br>
<a href="" id="image2Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);" style="display:none;">Get second picture</a><br>
<a href="" id="image3Button" class="ui-btn" onclick="getPhoto(pictureSource.PHOTOLIBRARY);" style="display:none;">Get third picture</a><br>
<img id="image1" style="display:none;width:25%;">
<img id="image2" style="display:none;width:25%;">
<img id="image3" style="display:none;width:25%;">
<label for="title">Title</label>
<input data-clear-btn="true" name="title" id="title" value="" type="text">
<input value="Continue" type="submit" id="adButton">

here is jquery code: 这是jquery代码:

multi_upload(user_id);

function multi_upload(user_id) {

    var image1 = "image1";
    var image2 = "image2";
    var image3 = "image3";
    if($('#image2').prop('src') == ''){
        // upload one file
        upload(user_id, image1, "true");
    }
    if($('#image3').prop('src') == ''){
        // upload two files
        upload(user_id, image1, "false");
        upload(user_id, image2, "true");
    }
    if($('#image3').prop('src') != ''){
        // upload three files
        upload(user_id, image1, "false");
        upload(user_id, image2, "false");
        upload(user_id, image3, "true");
    }
}

function upload(user_id, imagesrc, final) {
    var img = '';
    var imageURI = '';
    img = document.getElementById(imagesrc);
    imageURI = img.src;
    console.log("[imageURI] "+imageURI);
    var options = new FileUploadOptions();
    options.fileKey = "file";
    options.fileName = imageURI.substr(imageURI.lastIndexOf('/') + 1);
    options.mimeType = "image/jpeg";
    var params = {};
    params.timestamp = Math.round(+new Date()/1000);
    params.public_token = localStorage.getItem("token_public");
    params.hash = SHA1(params.timestamp+localStorage.getItem("token_private"));
    params.user_id = user_id;
    options.params = params;
    options.chunkedMode = false;
    var ft = new FileTransfer();
    if(final == "true"){
        ft.upload(imageURI, "http://www.example.com/api/index.php/privates/upload", finalwin, fail, options);
    }else{
        ft.upload(imageURI, "http://www.example.com/api/index.php/privates/upload", win, fail, options);
    }
}

If i upload two files for example, the code will upload two times the last selected picture. 例如,如果我上传两个文件,代码将上传最后一个选定图片的两倍。 the console give me the imageURI who looks like this: 控制台给我imageURI看起来像这样:

file:///storage/sdcard0/Android/data/fr.myproject.propro/cache/modified.jpg?1418726649440:500

I suppose that is a temporary file, so i presume when i select the last file, it delete the previous one... how can i find the real path of this images? 我想这是一个临时文件,所以我猜想当我选择最后一个文件时,它会删除前一个文件...我怎样才能找到这个图像的真实路径?

We recently sat with the same problem, and found that the cache file ( project/cache/modified.jpg ) was overriden (as you note) by a new selection, although FileTransfer.upload seems to treat it as two different files (presumably due to the ?-parameter) and thus uploads it twice. 我们最近遇到了同样的问题,发现缓存文件( project/cache/modified.jpg )被新选择覆盖(如你所注意到的),尽管FileTransfer.upload似乎将它视为两个不同的文件(可能是由于到#-parameter),然后上传两次。

As a workaround, we ended up renaming the files to include a timestamp before the name, such that modified.jpg?1418726649440 becomes 1418726649440modified.jpg , prior to uploading them: 作为一种解决方法,我们最终重命名文件以在名称前包含时间戳,以便在上传之前, modified.jpg?1418726649440变为1418726649440modified.jpg

function renameFile(src, callback) {
    var d = new Date();
    //find the FileEntry for the file on the device
    window.resolveLocalFileSystemURL(src, function(fileEntry) {
        //get the parent directory (callback gives a DirectoryEntry)
        fileEntry.getParent(function(parent) {
            //rename the file, prepending a timestamp.
            fileEntry.moveTo(parent, d.getTime() + fileEntry.name, function(s) {
                //Callback with the new URL of the file.
                callback(s.nativeURL);
            }, function(error) {
                alert('Error on moving file!');
                callback(src); //Fallback, use the src given
            });
        }, function(error) {
            alert('Error on getting parent!');
            callback(src); //Fallback
        });
    }, function(error) {
        alert('Error on resolveLocalFileSystemURI!');
        callback(src); //Fallback
    });
}

With src being the imageURI (ie path of the file), and callback being the function that uploads the file. src是imageURI(即文件的路径), callback是上传文件的函数。 (I should note that we're not entirely sure if we need the getParent call, as one could presumably get the DirectoryEntry by parsing src , but better safe than sorry) (我应该注意,我们并不完全确定我们是否需要getParent调用,因为可以通过解析src获取DirectoryEntry ,但是比对不起更安全)

NOTE: This requires the File plugin, and, depending both on your version of Cordova and File, may need to be edited somewhat (as the API has changed a little between versions). 注意:这需要File插件,并且根据您的Cordova和File版本,可能需要进行一些编辑(因为API在版本之间稍有变化)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM