I am developing an application for a radio station, have a section to download your audio programs. Works perfectly on Android and iOS, but Windows Phone starts downloading (very very slow) and after a time the application breaks showing this error message
An exception of type 'System.Runtime.InteropServices.SEHException' occurred in Unknown Module. and wasn't handled before a managed/native boundary
Also during the short time that is downloading shows this message
DispatchFileTransferProgress : FileTransfer1024600849
The size of the files you download is between 15 and 20 mb.
Anyone know what happens? Thanks in advance
Edited for add code
Code:
window.requestFileSystem(
LocalFileSystem.PERSISTENT,
0,
function onRequestFileSystemSuccess(fileSystem) {
fileSystem.root.getDirectory(
'Radio', //Directorory was created when apps starts firs time
{ create: true, exclusive: false },
function(directoryEntry) {
directoryEntry.getFile(
'dummy.html',
{ create: true, exclusive: false },
function onGetFileSuccess(fileEntry) {
var path = fileEntry.toURL().replace('dummy.html', '');
var fileTransfer = new FileTransfer();
fileEntry.remove();
var url = record.get('file');
var file = record.get('file').split('/');
fileTransfer.onprogress = function(progressEvent) {
if (progressEvent.lengthComputable) {
console.log('Downloaded: ' + ((progressEvent.loaded / progressEvent.total) * 100));
}
};
fileTransfer.download(
url,
path + file[file.length - 1],
function(file) {
console.log('Downloaded!');
},
function(error) {
console.log("download error source " + error.source);
console.log("download error target " + error.target);
console.log("upload error code" + error.code);
}
);
},
function fail(error) {
console.log('Get file ' + error.code);
}
);
},
function fail(error) {
console.log('Get directory ' + error.code);
}
);
},
function fail(error) {
console.log('Get file system ' + error.code);
}
);
Miguel
Related
I need to upload a file from my Cordova mobile application. I used cordova-file-transfer plugin before it was deprecated. I just found the plugin https://github.com/spoonconsulting/cordova-plugin-background-upload. It looks great, a file is sent and stored on my server correctly, but no plugin event is fired. Instead of that, the plugin throws exception "Thread 14: EXC_BREAKPOINT (code=1, subcode=0x1b71f4150)" in setupStorage.m file line 67:
if (![persistentStoreCoordinator addPersistentStoreWithType:NSSQLiteStoreType configuration:nil URL:storeURL options:nil error:&error])
NSLog(#"error setting up core data: %#", error);
and the console shows message "File upload exited with error code (null)". Any idea, how to fix it or any other way, how to upload a file using Cordova?
My Cordova code is:
var uploader = FileTransferManager.init({parallelUploadsLimit: 2}, event => {
if (event.state == 'UPLOADED') {
console.log("upload: " + event.id + " has been completed successfully");
console.log(event.statusCode, event.serverResponse);
} else if (event.state == 'FAILED') {
if (event.id) {
console.log("upload: " + event.id + " has failed");
} else {
console.error("uploader caught an error: " + event.error);
}
} else if (event.state == 'UPLOADING') {
console.log("uploading: " + event.id + " progress: " + event.progress + "%");
}
});
var payload = {
id: "c3a4b4c7-4f1s-4c69-a951-773602e269fb",
filePath: targetFile.nativeURL,
fileKey: "file",
serverUrl: url,
notificationTitle: "Uploading images",
headers: {
api_key: "asdasdwere123sad"
},
parameters: params
};
uploader.startUpload(payload);
In my serverless app, I want to create pdf which is generated dynamically and then upload that created pdf into aws s3. My problem is, when a url is returned to client-side code from server, uploaded url doesn't working. My code is given below:
Client-side javascript code (angular.js)
$scope.downloadAsPDF = function() {
// first I have to sent all html data into server
var html = angular.element('html').html(); // get all page data
var service = API.getService();
service.downloadPdf({}, { html : html }, // api call with html data
function(res) {
console.log("res : ", res);
window.open(res.url); // open uploaded pdf file
// err: The server replies that you don't have permissions to download this file
// HTTP/1.1 403 Forbidden
}, function(err) {
console.log("err : ", err);
});
};
Serverless Code
var fs = require('fs');
var pdf = require('html-pdf');
var AWS = require("aws-sdk");
var s3 = new AWS.S3();
module.exports.handler = function(event, context) {
if (event.html) { // client html data
AWS.config.update({
accessKeyId: 'xxxxxxxxxxxxxxxxx',
secretAccessKey: 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
region: 'xxxxxxxxxxxxxxxxxxxx'
});
var awsInfo = {
bucket: 'xxxxx-xxxxxx'
};
var baseUrl = 'https://s3-my-region.amazonaws.com/s3-upload-directory';
var folderRoot = 'development/pdf';
// unique file name
var output_filename = Math.random().toString(36).slice(2) + '.pdf';
// file created directory
var output = '/tmp/' + output_filename;
pdf.create(event.html, options).toStream(function(err, stream) {
if( err ) {
console.log('pdf err : ', err);
} else {
writeStream =fs.createWriteStream(output);
s3.putObject({
Bucket : awsInfo.bucket,
Key : folderRoot + '/' + output_filename,
Body : fs.createReadStream(output),
ContentType : "application/pdf"
},
function(error, data) {
if (error != null) {
console.log("error: " + error);
} else {
// upload data: { ETag: '"d41d8cd98f00b204e9800998ecf8427e"' }
console.log('upload data : ', data);
return cb(null, {
// return actual aws link, but no file
// ex: 'https://s3-my-region.amazonaws.com/s3-upload-directory/output_filename.pdf
url: baseUrl + '/' + output_filename
});
}
});
}
}
};
I've solve my problem. I was trying to upload pdf before I generate pdf. I have solve this problem using the following code:
pdf.create(event.html, options).toStream(function(err, stream) {
if (err) {
console.log('pdf err : ', err);
} else {
var stream = stream.pipe(fs.createWriteStream(output));
stream.on('finish', function () {
s3.putObject({
Bucket : awsInfo.bucket,
Key : folderRoot + '/' + output_filename,
Body : fs.createReadStream(output),
ContentType : "application/pdf"
},
function(error, data) {
if (error != null) {
console.log("error: " + error);
return cb(null, {
err: error
});
} else {
var url = baseUrl + '/' + output_filename
return cb(null, {
url: url
});
}
});
});
}
});
I have done similar kind of thing before. I want a few clarifications from you and then I will be able to help you better.
1) In your code (server side), you have mentioned in the callback function that actual aws link is getting returned.
Are you sure that your file is getting uploaded to Amazon s3. I mean did you check your bucket for the file or not?
2) Have you set any custom bucket policy on Amazon s3. Bucket policy play an important role in what can be downloaded from S3.
3) Did you check the logs to see exactly which part of code is causing the error?
Please provide me this information and I think the I should be able to help you.
if we don't want to upload at s3 just return generated file from aws-lambda.
We are working on an iOS project in Ionic. We want the cordova screenshot plugin to fire on ionicview enter (when first entering the app), and then use cordova file transfer to send the screenshot.
The below code does not work when first time entering the view. When we leave the view and come back however, it does send a screenshot.
However, the first logAction DOES fire on the first time entering this view while the second logAction does not. When entering this view for the second time, both logActions are fired.
This is the part of the code i am referring to:
$scope.$on('$ionicView.enter', function () {
$scope.logAction({
"Message": "Login screen succesfully entered",
"Succeeded": true,
"transaction": 1,
"Category": "Info",
"Device": 0,
})
$cordovaScreenshot.capture().then(function(filepath){
$scope.logAction({
"Message": filepath,
"Succeeded": true,
"transaction": 1,
"Category": "Info",
"Device": 0,
})
$cordovaScreenshot.send(filepath);
});
});
This is the cordovaScreenshot file
angular.module('testScreenshots.services', [])
.service('$cordovaScreenshot', ['$q',function ($q) {
return {
fileURL: "",
capture: function (filename, extension, quality) {
var randomNumber = Math.random();
console.log("" + randomNumber);
filename = "testPicture" + randomNumber.toString();
extension = extension || 'jpg';
quality = quality || '100';
var defer = $q.defer();
navigator.screenshot.save(function (error, res){
if (error) {
console.error(error);
defer.reject(error);
} else {
console.log('screenshot saved in: ', res.filePath);
this.fileURL = "file://" + res.filePath;
defer.resolve(res.filePath);
console.log("inside the save function: "+this.fileURL);
}
}, extension, quality, filename);
return defer.promise;
},
send: function(filepath){
var win = function (r) {
console.log("Code = " + r.responseCode);
console.log("Response = " + r.response);
console.log("Sent = " + r.bytesSent);
}
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);
}
var options = new FileUploadOptions();
options.fileKey = "file";
options.fileName = filepath.substr(filepath.lastIndexOf('/') + 1);
options.mimeType = "multipart/form-data";
options.chunkedMode = false;
options.headers = {
Connection: "close"
};
var ft = new FileTransfer();
ft.upload(filepath, encodeURI("http://192.168.6.165:8080//api/uploadpicture"), win, fail, options);
}
};
}])
The iOS simulator we use on the Mac had this issue. After trying to run it on a device the issue no longer affected us.
This issue thus seems to relate to using the iOS simulator.
There is a mobile app in which i want to delete the cached images downloaded by a particular user after log out.Suppose user1 logged in and download few images and user2 logged in and downloaded few images.User2 should not see downloaded images of any other user.
downloadFile : function(downloadLink, downloadFileName, downloadFileMimeType) {
$ionicLoading.show({
template: '<ion-spinner></ion-spinner>'
});
var accessToken = $window.localStorage.getItem(SYSTEM.AUTH_TOKEN);
var options = {
headers : {
'Authorization' : 'Bearer ' + accessToken
}
};
var ext;
if (downloadFileMimeType == 'application/pdf') {
ext = '.pdf';
} else {
ext = '.jpg';
}
var localPath;
if(ionic.Platform.isAndroid()){
localPath = cordova.file.externalCacheDirectory;
}else{
localPath = cordova.file.cacheDirectory;
}
localPath = localPath + downloadFileName.trim().replace(/\s+/g, '-') + ext;
var ft = new FileTransfer();
ft.download(downloadLink, localPath, function(entry) {
$ionicLoading.hide();
console.log("Downloading report on path - " + entry.toURL());
cordova.plugins.fileOpener2.open(entry.toURL(), downloadFileMimeType, {
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function(fileEntry) {
console.log('File opened successfully');
}
});
}, function fail(error) {
$ionicLoading.hide();
console.log("Error while downloading report with error code - " + error.code);
}, true, options);
}
You can store files downloaded by specific user under a user specific folder in device and delete the same when the other user logs in. Else you can follow some file naming convention specific to the users while storing the files in folder and delete those files specific to particular user when the other user logs in. The sample snippet for directory deletion and its contents using cordova file plugin is as follows:
function clearDirectory() {
if (ionic.Platform.isAndroid()) {
window.resolveLocalFileSystemURL(cordova.file.externalRootDirectory, onFileSystemDirSuccess, fail);
} else {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFileSystemDirSuccess, fail);
}
function onFileSystemDirSuccess(fileSystem) {
var entry = "";
if (ionic.Platform.isAndroid()) {
entry = fileSystem;
} else {
entry = fileSystem.root;
}
entry.getDirectory("DIRECTORY_TO_DELETE", {
create: true,
exclusive: false
},
function(entry) {
entry.removeRecursively(function() {
console.log("Remove Recursively Succeeded");
}, fail);
}, getDirFail);
}
function getDirFail(error) {
navigator.notification.alert("Error");
};
function fail(error) {
navigator.notification.alert("Error");
};
}
I am using streaming-s3 node-modules, which is working fine on my local machine.
But on live it doesn't seem working. I have https enabled on live server.
if i disabled https on live server my uploading is working fine .
Here is my code
exports.uploadFile = function (fileReadStream, awsHeader, cb) {
//set options for the streaming module
var options = {
concurrentParts: 2,
waitTime: 20000,
retries: 2,
maxPartSize: 10 * 1024 * 1024
};
//call stream function to upload the file to s3
var uploader = new streamingS3(fileReadStream, config.aws.accessKey, config.aws.secretKey, awsHeader, options);
//start uploading
uploader.begin();// important if callback not provided.
// handle these functions
uploader.on('data', function (bytesRead) {
console.log(bytesRead, ' bytes read.');
});
uploader.on('part', function (number) {
console.log('Part ', number, ' uploaded.');
});
// All parts uploaded, but upload not yet acknowledged.
uploader.on('uploaded', function (stats) {
console.log('Upload stats: ', stats);
});
uploader.on('finished', function (response, stats) {
console.log(response);
logger.log('info', "UPLOAD ", response);
cb(null, response);
});
uploader.on('error', function (err) {
console.log('Upload error: ', err);
logger.log('error', "UPLOAD Error: ", err);
cb(err);
});
Any idea about this
Thanks
You need to enable ssl in your client, add sslEnabled option like following
var options = {
sslEnabled: true,
concurrentParts: 2,
waitTime: 20000,
retries: 2,
maxPartSize: 10 * 1024 * 1024
};
you can check more about the options you can use http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/Config.html