javascript - Cordova multiple image upload with File Transfer -
i'm on cordova , jquery mobile project.
i've been able upload 1 image file transfer plugin. try upload 2 or 3 images following.
here html code:
<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 jquery code:
multi_upload(user_id); function multi_upload(user_id) { var image1 = "image1"; var image2 = "image2"; var image3 = "image3"; if($('#image2').prop('src') == ''){ // upload 1 file upload(user_id, image1, "true"); } if($('#image3').prop('src') == ''){ // upload 2 files upload(user_id, image1, "false"); upload(user_id, image2, "true"); } if($('#image3').prop('src') != ''){ // upload 3 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 upload 2 files example, code upload 2 times last selected picture. console give me imageuri looks this:
file:///storage/sdcard0/android/data/fr.myproject.propro/cache/modified.jpg?1418726649440:500
i suppose temporary file, presume when select last file, delete previous one... how can find real path of images?
we sat same problem, , found cache file (project/cache/modified.jpg
) overriden (as note) new selection, although filetransfer.upload seems treat 2 different files (presumably due ?-parameter) , uploads twice.
as workaround, ended renaming files include timestamp before name, such modified.jpg?1418726649440
becomes 1418726649440modified.jpg
, prior uploading them:
function renamefile(src, callback) { var d = new date(); //find fileentry file on device window.resolvelocalfilesystemurl(src, function(fileentry) { //get parent directory (callback gives directoryentry) fileentry.getparent(function(parent) { //rename file, prepending timestamp. fileentry.moveto(parent, d.gettime() + fileentry.name, function(s) { //callback new url of file. callback(s.nativeurl); }, function(error) { alert('error on moving file!'); callback(src); //fallback, use src given }); }, function(error) { alert('error on getting parent!'); callback(src); //fallback }); }, function(error) { alert('error on resolvelocalfilesystemuri!'); callback(src); //fallback }); }
with src
being imageuri (i.e. path of file), , callback
being function uploads file. (i should note we're not entirely sure if need getparent
call, 1 presumably directoryentry
parsing src
, better safe sorry)
note: requires file plugin, and, depending both on version of cordova , file, may need edited (as api has changed little between versions).
Comments
Post a Comment