Comments (23)
I had the same problem trying to open a PDF located in the www/ folder. So to open it in android: I copied it to the external data directory and then I opened the new file:
(uri is /www/path/to/file.pdf)
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + uri, function(fileEntry) {
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
fileEntry.copyTo(dirEntry, 'file.pdf', function(newFileEntry) {
cordova.plugins.fileOpener2.open(newFileEntry.nativeURL,'application/pdf');
});
});
});
from cordova-plugin-file-opener2.
Thanks to @japostigo-atsistemas solution!
I tweaked it a bit, here's what I recommend:
- use
window.open
instead offileOpener2.open
- no need to declare mimetype
- needs cordova-plugin-inappbrowser plugin instead of FileOpener2 plugin
- use
uri.split('/').pop()
to keep the original filename when copying
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + uri, function(fileEntry) {
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
fileEntry.copyTo(dirEntry, uri.split('/').pop(), function(newFileEntry) {
window.open(newFileEntry.nativeURL, '_system');
});
});
});
from cordova-plugin-file-opener2.
@Balaji-yogy I had the same issue. I changed from window.TEMPORARY to LocalFileSystem.PERSISTENT and it worked.
from cordova-plugin-file-opener2.
Try to put your pdf file in:
/storage/emulated/0/1.pdf
and call your function as:
openPDF('/storage/emulated/0/1.pdf');
Does that work?
from cordova-plugin-file-opener2.
hi @pwlin , i try to open pdf from local storage with path from cordova file plugin.
here my code :
var filePDF = 'tes.pdf';
cordova.plugins.fileOpener2.open(
cordova.file.dataDirectory+filePDF, // path file:///data/data/<app-id>/files/tes.pdf
'application/pdf',
{
error : function(e) {
alert('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
alert('file opened successfully');
}
}
);
It return"file opened successfully"
but from adobe pdf show alert that "The document path is not valid"
can you help me, please?
Thanks
from cordova-plugin-file-opener2.
Hi @pwlin,
I tried opening pdf in my windows 8.1 phone. the file path is //app-folder/slip-201432-1-2015.pdf
and also have adobe acrobat installed in my phone.
Whenever i click open pdf button it goes into error function and show Error status: undefined - Error message: undefined
, any idea what would be reason?
Thanks in advance
from cordova-plugin-file-opener2.
I am using the same code as this
var fs;
function fsSuccess(fileSystem)
{
fs = fileSystem;
}
function fsFail(event)
{
console.log(event.target.error.code);
}
function openPDF(file) { window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);
file = fs.root.toURL() + "pubs/2015/mypdf.pdf";
cordova.plugins.fileOpener2.open(
file, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
'application/pdf',
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
$('#errorpdf').html('Error status: ' + e.status + ' - Error message: ' + e.message+' - File : '+file);
},
success : function () {
console.log('file opened successfully');
$('#errorpdf').html('file opened successfully');
}
}
);
}
It throws me the error: Cannot read property 'root' of undefined
My path for the pdf files: video/www/pubs/2015/mypdf.pdf
My HTML file path: video/www/publications.html
Please advice me what wrong on this and how could I resolve it.
Thanks!
from cordova-plugin-file-opener2.
Hi @pwlin,
Can you please brief me how I would do this
/*
Try to put your pdf file in:
/storage/emulated/0/1.pdf
and call your function as:
openPDF('/storage/emulated/0/1.pdf'); */
How I could put my local pdf file in /storage/emulated path.
Thanks!
from cordova-plugin-file-opener2.
Finally, I did it like this, with 2 plugins :
<gap:plugin name="io.github.pwlin.cordova.plugins.fileopener2" version="1.0.11" />
<gap:plugin name="com.gkcgautam.asset2sd" version="2.0.0" />
Here my whole code not cleaned (sorry), if you need it, you have tu remove some lines such as visual "feedbacks", etc.
/* First of all... copy a directory to local storage */
function copyPDF(file) {
var timeNow = Math.floor(Date.now() / 1000);
var timeLastDataBaux = localStorage.getItem("bauxPDFDate");
// Don't copy the PDF for 15 min., it will be faster
if(timeLastDataBaux > (timeNow-900)) // (60 * 15 = 900) 15 minutes de cache
{
if(localStorage.getItem("bauxPDFDate") !== null)
{
// Display buttons to open PDFs
$('#baux_page_loading').fadeOut('slow', function() {
$('#baux_page_loaded').fadeIn('fast');
});
return true;
}
else
{
// ?
console.log('First copy of files...');
}
}
else
{
// Else, we have no data, or too old ? normal way => we launch the copy etc.
console.log('Files too old...');
}
asset2sd.copyDir({
asset_directory: file, // 'www/pdf'
destination_directory: "MyDir"
},
function(result)
{
// SUCCESS !
$('#baux_page_loading').html('<p>✓ Well loaded !</p>');
var timestamp = Math.floor(Date.now() / 1000);
localStorage.setItem('bauxPDFDate', timestamp);
// Display button to open PDFs
$('#baux_page_loading').fadeOut('slow', function() {
$('#baux_page_loaded').fadeIn('fast');
});
}, function()
{
// ERROR
$('#baux_page_loading').fadeOut('normal');
$('#errorpdf').html('<div class="error">Impossible to load PDF files on the internal storage space or SD card (erreur : '+file+' - '+result+'). Please see our website <a onclick="http://www.example.com" class="coloredTap">www.example.com</a> to download its PDF.</div>');
});
}
/* Second, this is the way to open local file already copied with asset2sd */
function openPDF(filename, btn) {
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);
file = fs.root.toURL() + filename;
//$(btn).fadeOut('fast', function() { $(btn).fadeIn('fast'); });
$(btn).after( '<img src="images/loader.gif" align="absmiddle" id="littleLoader">' );
cordova.plugins.fileOpener2.open(
file, // You can also use a Cordova-style file uri: cdvfile://localhost/persistent/Download/starwars.pdf
'application/pdf',
{
error : function(e) {
//console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
$('#errorpdf3').html('<div class="error">Cannot open PDF in this path (erreur : ' + e.status + ' - msg : ' + e.message+' - chemin : '+file+'). Please see our website <a onclick="http://www.example.com" class="coloredTap">www.example.com</a> to download it.</div>');
},
success : function () {
//console.log('file opened successfully');
setTimeout(function(){
$('#littleLoader').fadeOut('normal');
$('#littleLoader').remove();
}, 1500);
//$('#errorpdf3').html('file opened successfully');
}
}
);
}
from cordova-plugin-file-opener2.
Hi,
Thanks for your reply. In my app, all my pdfs are resided at my asset folder and the path is www/pdf/2015/mypdf.pdf
Since my pdf files are located in my local folder, I implemented the following code in my html page.
(Is this the correct way am I using?)
function openPDF(file) {
var fs;
function fsSuccess(fileSystem)
{
fs = fileSystem;
}
function fsFail(event)
{
console.log(event.target.error.code);
}
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);
file = fs.root.toURL() + file;
cordova.plugins.fileOpener2.open(
file,
'application/pdf',
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message + '- Error:' + JSON.stringify(e));
// $('#errorpdf').html('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
alert("pass");
console.log('file opened successfully');
// $('#errorpdf').html('file opened successfully');
}
}
);
}
Play PDF on Android
For the above code, im getting the error, " Cannot read property 'root' of undefined".And also I have tried in this way:
cordova.plugins.fileOpener2.open(
'file:///android_asset/www/pdf/2015/mypdf.pdf',
'application/pdf',
{
error : function(e) {
console.log('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
console.log('file opened successfully');
}
}
);
It throws me "File not found" error.
What I did wrong in my both code? Please advice how I can fix this?
from cordova-plugin-file-opener2.
Hi Nicolas,
I have implemented the code as follow yours
/*First of all... copy a directory to local storage */
function copyPDF() {
var bauxPDFDate = Math.floor(Date.now());
localStorage.setItem("bauxPDFDate", JSON.stringify(bauxPDFDate));
var timeNow = Math.floor(Date.now() / 1000);
var timeLastDataBaux = localStorage.getItem("bauxPDFDate");
// Don't copy the PDF for 15 min., it will be faster
if(timeLastDataBaux > (timeNow-900)) // (60 * 15 = 900) 15 minutes de cache
{
if(localStorage.getItem("bauxPDFDate") !== null)
{
// Display buttons to open PDFs
$('#baux_page_loading').fadeOut('slow', function() {
$('#baux_page_loaded').fadeIn('fast');
});
return true;
}
else
{
// ?
console.log('First copy of files...');
}
}
else
{
// Else, we have no data, or too old ? normal way => we launch the copy etc.
console.log('Files too old...');
}
asset2sd.copyDir({
asset_directory: 'www/pdf',
destination_directory: "MyPdfDir"
},
function(result)
{
// SUCCESS !
$('#baux_page_loading').html('<p>✓ Well loaded !</p>');
var timestamp = Math.floor(Date.now() / 1000);
localStorage.setItem('bauxPDFDate', timestamp);
// Display button to open PDFs
$('#baux_page_loading').fadeOut('slow', function() {
$('#baux_page_loaded').fadeIn('fast');
});
}, function()
{
// ERROR
$('#baux_page_loading').fadeOut('normal');
$('#errorpdf').html('<div class="error">Impossible to load PDF files on the internal storage space or SD card (erreur : '+file+' - '+result+'). Please see our website <a onclick="http://www.example.com" class="coloredTap">www.example.com</a> to download its PDF.</div>');
});
}
copyPDF();
var fs;
function openPDF(filename) {
function fsSuccess(fileSystem)
{
fs = fileSystem;
alert("fs==>" +fileSystem.name);
}
function fsFail(event)
{
console.log(event.target.error.code);
}
window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, fsSuccess, fsFail);
file = 'file:///storage/sdcard0/MyPdfDir/' +filename; /*If I use fs.root.toURL().filename, it returns this path - "file:///storage/sdcard0" and it still throw the error "File not found" and also shows "Cannot read property 'root' of undefined". */
cordova.plugins.fileOpener2.open(
file,
'application/pdf',
{
error : function(e) {
alert("Error:" + JSON.stringify(e));
console.log('Error status: ' + e.status + ' - Error message: ' + e.message + '- Error:' + JSON.stringify(e));
$('#errorpdf').html('Error status: ' + e.status + ' - Error message: ' + e.message);
},
success : function () {
alert("pass");
console.log('file opened successfully');
$('#errorpdf').html('file opened successfully');
}
}
);
}
<body>
<p><button id="baux_page_loaded" onclick="openPDF('testpdf.pdf');">Play PDF on Android</button></p>
<!--<a href="javascript:void(0);" onclick="openPDF();"> Open PDF</a>-->
<div id="baux_page_loading"> Loading</div>
<div id="errorpdf"></div>
</body>
file = 'file:///storage/sdcard0/MyPdfDir/' +filename;
If I use fs.root.toURL().filename, it returns this path - "file:///storage/sdcard0" and it still throw the error "File not found" and also shows "Cannot read property 'root' of undefined".
It seems "asset2sd.copyDir" plugin is not being fire anything, neither goes to success nor failure call.
What I did wrong in this.
I have created the directory called MyPdfDir, but when I access this path, it says file not found. "file:///storage/sdcard0/MyPdfDir/testpdf.pdf"
Please advice me how I can declare the file path name.
from cordova-plugin-file-opener2.
Im facing the same problem using this plugin.
How to resolve it
from cordova-plugin-file-opener2.
If any spaces in the path it will add %20.So the file is not found in that case.If in that case decode the path using javascript method "decodeURIComponent" .like decodeURIComponent(path);
from cordova-plugin-file-opener2.
After hours of trying to open a PDF in Android, @japostigo-atsistemas 's solution worked well for me. Thanks!
from cordova-plugin-file-opener2.
@japostigo-atsistemas +1 to your solution mate. Thanks
from cordova-plugin-file-opener2.
Hi @japostigo-atsistemas,
I tried this coding with ionic and it doesn't work for me. can you send me the detailed code so that i will keep that as reference for my project
from cordova-plugin-file-opener2.
Hi expert,
I am also facing the same issue while open tiff image in android application using cordova i am getting the same error as file not found error code 9 . i am attaching sample code here. Can you please help me the correct solution as i am facing the issue since long time and not able to proceed further.
cordovagetattachment.txt
Thanks&Regards,
Saurabh
from cordova-plugin-file-opener2.
@japostigo-atsistemas your solution wasn't working for me so I added a check for errors and it's throwing
DOMException: A URI supplied to the API was malformed, or the resulting Data URL has exceeded the URL length limitations for Data URLs
Any ideas?
from cordova-plugin-file-opener2.
@brainwind-software your approach does not work with Android 7 (API Level 24+) anymore, so I suggest to stick with the file-opener way (see #125)
from cordova-plugin-file-opener2.
I have an ionic app in which I used inAppBrowser to open a PDF file (downloaded in cordova.file.externalDataDirectory).
Now with Android SDK > 24 I get a FileUriExposedException.
And same error with FileOpener2...
What could I do ?
from cordova-plugin-file-opener2.
I wonder if a special folder exists where app could store files and open them without throwing FileUriExposedException.... any idea?
from cordova-plugin-file-opener2.
I had the same problem trying to open a PDF located in the www/ folder. So to open it in android: I copied it to the external data directory and then I opened the new file:
(uri is /www/path/to/file.pdf)
window.resolveLocalFileSystemURL(cordova.file.applicationDirectory + uri, function(fileEntry) { window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) { fileEntry.copyTo(dirEntry, 'file.pdf', function(newFileEntry) { cordova.plugins.fileOpener2.open(newFileEntry.nativeURL,'application/pdf'); }); }); });
Not working for me sir,as onclick function of <button>
window.resolveLocalFileSystemURL(cordova.file.externalDataDirectory, function(dirEntry) {
fileEntry.copyTo(dirEntry, '18main.pdf', function(newFileEntry) {
cordova.plugins.fileOpener2.open(newFileEntry.nativeURL,'application/pdf');
});
});
});"
from cordova-plugin-file-opener2.
Going to close this issue as the person who opened the issue in the first place resolved it some time ago. If you are experiencing what you believe to be a related problem, please raise a related issue.
from cordova-plugin-file-opener2.
Related Issues (20)
- FileOpener2 error in electron 11 HOT 1
- Plugin is not handling Cookies in React project HOT 1
- Add documentation for error codes
- USB PORT PARAM HOT 1
- doesnt work with IOS and assets-library
- Usage in Capacitor HOT 1
- pdf opened in an external app cannot save changes HOT 1
- Cannot open file while is at app data location '/data/data/<app_id>/cache' HOT 2
- unable to open application/pdf format in Android device (Samsung Galaxy S21 and Samsung Galaxy S21+) HOT 2
- android.support.v4.content.FileProvider not found HOT 7
- Relative URIs are not supported.
- remove: android.permission.REQUEST_INSTALL_PACKAGES from plugins.xml HOT 85
- Text Open file in always localized in english (Android) HOT 4
- There was a problem parsing the package for android install apk HOT 1
- Can't edit file HOT 2
- Getting error after updating plugin to 4.0.0 on Android Studio HOT 2
- Unable to open some MP4 files on iOS? HOT 1
- This plugin has a maintained forked repo now - please use and follow HOT 4
- Error: Current working directory is not a Cordova-based project.
- iPadOS not supported
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cordova-plugin-file-opener2.