uploading files using filetransfer plugin on cordova ios - ios

Im using jquery with cordova to build an app , on android everything works like a charm , but on ios the uploading is not working using filetransfer plugin .
this is the function i use to upload the picture that the user select already using the file plugin :
function uploadFile() {
var fileURI = $("#selectedPicture").attr("src");
var options = new FileUploadOptions();
options.fileKey = "file";
options.mimeType = "image/jpeg";
options.fileName = fileURI.substr(fileURI.lastIndexOf('/') + 1);
var params = new Object();
options.params = params;
options.chunkedMode = false;
var ft = new FileTransfer();
ft.upload(fileURI, serviceURL+"uploadFile",function(r){
alert(r.response);
}, fail,
options);
}
var fail = function (error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
on the console i don't get any error or message .
i added those to lines on my infoplist file :
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>
PS 1 : i use ios 9.2
PS 2 : i use cordova 6 .
PS 3 : the normal ajax works great only the upload thats not working .
PS 4 : i test the code using the latest ios simulator .

the problem was from the image size i tried to upload .
i just selected an other image and i got what i was looking for .

Related

Open local files in ionic iOS

I'm trying to open an already present .pdf file on iOS [I can see the files in iBooks].
I use Ionic and the file-opener2 Cordova plugin - I use the latest versions of Cordova and the plugin.
This is the code that works perfectly on Android:
$scope.openPDF = function() {
alert("OK");
$cordovaFileOpener2.open(
'/storage/emulated/0/Download/pdf/name.pdf', // Any system location, you CAN'T use your appliaction assets folder
'application/pdf'
).then(function() {
console.log('Success');
}, function(err) {
console.log('An error occurred: ' + JSON.stringify(err));
});
};
Yes, I want to be a local file without having to download it, this is a show off app that won't have internet connection where will be running for just one day. So the files have to be local.
I have no idea how to open the file paths like in Android. Running on iOS 9.3.3., iPad Pro. Also, the device is not jail-broken.
Edit:
Well, now I'm trying to move the files from the app's folder to another one, so I can open it from there. Again, all of the code works on Android.
function openPDF(uri) {
var filePath = cordova.file.applicationDirectory + 'www/pdf/fichas/name.pdf';
alert(filePath);
window.resolveLocalFileSystemURL(filePath, function(entry) {
var fileTransfer = new FileTransfer();
var targetFile = cordova.file.externalDataDirectory + entry.name;
fileTransfer.download(
entry.toURL(),
targetFile,
function(entry) {
console.log("download complete: " + entry.toURL());
console.log("targetFile: " + targetFile);
cordova.plugins.fileOpener2.open(
targetFile,
'application/pdf',
{
error : function(error){ alert('open error ' + JSON.stringify(error)) },
success : function(){ }
}
);
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
}, function(error){ alert('error resolveLocalFileSystemURI ' + JSON.stringify(error)) });
};
alert(filePath); * returns: file:///var/containers/Bundle/Application/xxxx-xxxx-xxxx-xxxxxxxxxxxxxxx/NAME.app/www/pdf/name.pdf
I don't know where it fails though. I won't open anything.
edit2:
I get a FileTransfer error indeed. "
Could not create path to save dowloaded file.

how to run a cordova/phonegap plugin asynchrounously - fileTransfer plugin

I have used cordova filetransfer plugin. I am uploading a file using fileTransfer plugin's upload function.
The problem is that upload works synchronously and application UI freezes while the file is being uploaded by this plugin.
I want the file upload to run asynchronously and user should be able to interact on UI while upload is in progress.
My cordova application is targeted for iOS and Windows.
I have used cordova filetransfer plugin. I am uploading a file using fileTransfer plugin's upload function.
The problem is that upload works synchronously and application UI freezes while the file is being uploaded by this plugin.
I want the file upload to run asynchronously and user should be able to interact on UI while upload is in progress.
My cordova application is targeted for iOS and Windows.
My javascript function which calls the cordova file-transfer plugin is as below:
var uploadVideo = function(path) {
//UPLOAD VIDEO TO SERVER
var fileURL = path;
var win = function(r) {
console.log("Code = " + r.responseCode);
};
var fail = function(error) {
console.log("An error has occurred: Code = " + error.code);
};
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = fileURL.substr(fileURL.lastIndexOf('/') + 1);
options.mimeType = "text/plain";
var params = {};
params.value1 = "test";
params.value2 = "param";
options.params = params;
var ft = new FileTransfer();
ft.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
var perc = Math.round(progressEvent.loaded / progressEvent.total * 100);
$('progress').val(perc);
}
};
ft.upload(fileURL, encodeURI("http://<serverpath>/<endpoint>"), win, fail, options);
};
As soon as this script/function is invoked, user is unable to interect with html page. Page kind of freezes and user can't input any text in text boxes or any other UI interaction.

Phonegap / Cordova File Transfer Upload Image as Base64 String not working in ios

I'm working on uploading image as base64 image stream from phonegap/cordova application, it works perfectly fine in Android but not working in ios, however in ios it works when the image is as png/jpg but when it is as base64 image stream its not working, Please can anybody help me out with the same.
Here is my code for the same,
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = "test.jpg";
options.mimeType = "image/jpeg";
options.chunkedMode = false;
options.params = {
"key": "test.jpg",
"AWSAccessKeyId": awsKey,
"acl": acl,
"policy": policyBase64,
"signature": signature,
"Content-Type": ""
};
var ft = new FileTransfer();
ft.upload(imageURI, s3URI + "",
win, fail, options);
}
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
alert(r.response);
}
function fail(error) {
alert("An error has occurred: Code = " + error.code);
}
Version 1.5.0 of the file transfer plugin has already added that functionality. The patch was applied 14 Dec 2015.
You can see the commit here.
Upgrading the plugin solved the problem in my case at least.
Set chunkMode true when use base64 for upload using phonegap
I set chunkMode true but still file is not transferring on server .It seems that cordova file transfer plugin does not support base64 image for upload on iOS.

File Transfer Download with Cordova 3.4 for iOS 7

I'm a little confused with the filetransfer method of Cordova for iOS. (I had a version working with Android)
Apparently, I don't set the destination folder properly. target:null Could not create target file Note that I assume that the directory exists as it is created with success earlier in the script.
According to the Cordova documentation, I should use a entry.toURL to get the right path.
function download(filename){
var localPath = rootFS.toURL+'contentImages/'+filename;
var fileTransfer = new FileTransfer();
fileTransfer.download(encodeURI('http://myValidatedSource.com/'+filename),
localpath,
function(entry){
console.log('download completed for '+entry.fullPath);
},
function(error){
console.log(error);
}
);
}
I also tried this:
alert(rootFS.fullPath); ==> "/"
and
alert(rootFS.toURL); ==> "function(){
if (this.nativeURL){
return this.nativeURL;
}
return this.toInternalURL()|| "file://localhost"+this.fullPath";
}"
I was not so far...
var localPath = rootFS().toURL+'contentImages/'+filename;
instead of
var localPath = rootFS.toURL+'contentImages/'+filename;

iOS / Phonegap : using FileTransfer response

first, sorry if my english is bad.
Then, here is my issue:
I try to make an app for iOS with PhoneGap 2.8.1 . Evrything is going well on Android, now I just want to move my code on Xcode to compile on iPad or iPhone.
The probleme I have is with FileTransfer, when I upload an image. The transfer itself has no problem, as it finishes with a response code 200, so it's a success.
Then, my serve is supposed to return back an XML file, handled by the javascript code. The server works fine, because I have absolutly no problem with Android devices.
So, I made a lot of tests, and it appears that the app has a problem with the success callback or the FileTransfer.upload(). When I just put a simple console.log('success') in this callback function, it's fine. But when I try to use the FileUploadResult object, nothing happens.
And the weirdest thing is, that when I press the main button of the iPad or iPhone, in order to close my app, I see all my logs displayed in the debug window, such as console.log("Code = " + r.responseCode);, like in the PhoneGap example. And here, I can finally see that I actually receive the server response, but it's like something blocks it until I close the app.
Here is that part of the code :
function onPhotoDataSuccess(imageURI) {
console.log("---> Image: "+imageURI);
// File Transfer
var options = new FileUploadOptions();
options.fileKey="file";
options.chunkedMode = false;
options.fileName=imageURI.substr(imageURI.lastIndexOf('/')+1);
options.mimeType="image/jpeg";
var ft = new FileTransfer();
ft.upload(imageURI, encodeURI(window.localStorage.getItem("wsURL")), win, fail, options);
// Transfer succeeded
function win(r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
// Handle the XML response
}
else {
alert("No response. Please retry");
}
}
// Transfer failed
function fail(error) {
alert("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
}
}
Thanks for your help

Resources