In my Ionic app , Using cordova-fileChooser plugin i am able to get the Uri of the selected File .
Then Using cordova-plugin-filepath i got the absolute path of the file from the nativeURL on the phone
How do i get the file object from this path or Uri ?
I want to append it to a formData and post it .
Use cordova file transfer plugin.In this plugin you need to give the file url to upload.Reference is here link
Related
Good Afternoon,
Scenario: I am having pdf's file on local system and when user click the button, I want it to get downloaded. But after getting download the file on opening shows corrupted. While posting this query I notice the file which is getting download is of 1KB and original file size is 28KB. I didn't understand why ?
I am creating the excel file at backend and saving at user location as due to huge data it is taking a lot of time so once file is created, the user can download the file.
Below are the codes of JS .
function download() {
var filename="output.pdf";
var element = document.createElement('a');
var fileloc="C:\\ebooks\\PDF\\abc.pdf";
element.setAttribute('href','data:text/plain;charset=utf-8, ' +
encodeURIComponent(fileloc));
element.setAttribute('download', filename);
document.body.appendChild(element);
element.click();
}
Original file is good. Please correct me what I am doing it wrong .
The data: scheme URL you are creating resolves to a plain text document (not a PDF) containing the text C:\ebooks\PDF\abc.pdf.
You are saving this file with a .pdf extension so when you try to open it, your PDF reader tries to read it as a PDF (which it isn't).
If you want to save the contents of the file at the path you specified then you need to:
Have the user select the file using a <input type="file"> (because JS is not allowed access to files on the user's disk unless they select them explicitly and generating the URL using the FileReader.readAsDataURL() method.
If you are creating the file on the server, as you said, then there is no need to construct a data: scheme URL, you can just use the https: scheme URL that points to the file on the server.
I create an funciton to generating an xlsx file with node4excel library. This function work fine and generate me new files. But I want to save in database a link to this files. I try using multer from express-multer but it does not work and do not save me files in multer destination.
my code:
//multer function
#UseInterceptors(FileInterceptor('files', multerOptions))
uploadFile(#UploadedFile() file){
console.log(file);
}
//generating new file with excel4node
workBook.write('files.xlsx')
//trying to save this file with using multer
this.uploadFile(workBook.write('raport.xls'));
but it not work :/
can someone tell me how to create an link to new files which can be stored in ./files folder?
To save excel file you need to follow points below
just use node4excel to create the file
then save this file with workBook.write('xxxx.xlsx'). but you need to change xxxx.xlsx to workBook.write(`${uuid_v4}.xlsx`). why we use uuid_v4 as file name ? to overcame overridning old file when save new file with same name
now we have excel file with unique name saved in specific path in own server. let's say full path for this file is /app/data/files/5fe3cc43-e1c3-4c1d-a907-b1636b57a892.xlsx. remember to save file in this path you need to use workBook.write(`/app/data/files/${uuid_v4}.xlsx`).
now we missing some thing how to download it ? simply we save file name in database like fileName: "5fe3cc43-e1c3-4c1d-a907-b1636b57a892.xlsx". after save it we use the same uuid_v4 to save file name in database.
now we need to create endpoint exposed to user. when request to it with file name we will check if this user have permission to download this file if ok we will send this file to him. so we need user to send the filename to know what file he need, token to check permission (identify this user).
so you didn't need to use multer. multer help you to upload file not create file then sent it to client.
NOTE: All points above around Node.js (backend).
Users of my application can select and crop images using Ionic Native - Crop. After they have cropped their image, I will have the URI of that image such as:
file:///storage/emulated/0/Android/data/com.myApp/cache/1535369478970-cropped.jpg?1535369482232
I want to use Ionic's File API, since it has a method readAsDataURL(path, file) which will convert the file to a base64 encoded data url, which is what I exactly need.
However, how would I properly separate the path and file from the URI of the file I have above so that readAsDataURL(path, file) is satisfied?
I also do not know what these numbers behind the .jpg?1535369482232 mean and I do not know what the name of the file would be or if it has a different directory on iOS since the URI above is provided from a test using Android Emulator.
P.S. I have tried calling the method with just the path above and no file name passed as second argument, but got the following error:
{"code":13,"message":"input is not a directory"}
How can I achieve the result I want for both iOS and Android file paths?
Your path refers 'cacheDirectory' this.file.cacheDirectory.
To get the file name.
const pathsplit = "file:///storage/emulated/0/Android/data/com.myApp/cache/1535369478970-cropped.jpg".split('/');
filename = pathsplit[pathsplit.length-1];
I am using PDF.js to open a PDF file currently and its working fine.
Previously I was using like this:
http://myapp.mycompany.com/web/viewer.html?file=http://myapp.mycompany.com/my-PDF-file.pdf
My Problem:
For some reasons, I really do not want to reveal the file path of my PDF file and want to move my PDF files outside root directory. Is there any other way to provide the pdf file to the viewer.html? or is this the oly way?
NOTE: I have moved all the PDF files outside my root directory. So How can I access the PDF now?
Yes, there is. Call some function instead (and you don't even need PDF.js):
http://www.example.com/getPDF.php?fileName=pdfname.pdf
function fileName() {
$fileName = $_GET['fileName'];
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename='{$fileName}'");
readfile(__DIR__."/../pdfs/private/{$fileName}");
}
Use rawurlencode to encode '/getPDF.php?filename=pdfname.pdf' part:
"http://myapp.mycompany.com/web/viewer.html?file=".rawurlencode("/getPDF.php?filename=".rawurlencode("pdfname.pdf"))
I have to read a file from the file system of my phone. Essentially its to check if the file contains a word. The file is located in the /sys folder of my phone. I know I can get the contents of the File using FileReader.readAsText(file) to get the contents as a string and then parse it for the word. But how do I make the "file" object to pass to the FileReader object. Thanks in advance!
Edit: Just wanted to add here:
Can I use the File constructor here?
var file = File("/path/to/file");
You can only read data from your app folder (via XMLHttpRequest) and from the SD Card. You cannot read data from any other place in a Firefox OS app.