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).
Related
I'm using a PDF viewer on the frontend which can only read .pdf URLs (it's not able to read blob directly). So I'll need to download those files to the user's computer first. Hypothetically, this is what I want to do:
// download blob
let pdf = fetch(mysite.com);
// store in a temporary folder
// maybe "%AppData%\Local\Google\Chrome\User Data\Default\Cache"?
browser.storeFile(pdf);
// retrieve the file from temporary folder and pass to PDF viewer
myPDFViewer.load('%AppData%\Local\Google\Chrome\User Data\Default\Cache\my-file.pdf')
The PDF viewer I'm using can load offline files perfectly fine, as long as I have the absolute path to the file (ending with .pdf). I have tried all sorts of things but still not able to generate such .pdf URLs from the fetched blob.
So, is it possible to download files, store them in a temporary folder internal to the browser (which will get cleaned up automatically), and generate absolute local paths to those files?
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 have a task where I need to export a json to csv via an endpoint. When accessing the endpoint, the route should return the .csv file with the data. Is there a way to do this without having to save the file locally?
If you use a File Interceptor on a route without specifying a storage option, the file will exist purely in memory and you will not need to save it to the disk.
I should warn however that if a uploaded file is very large, you can run out of memory. It's generally a better option to save to the disk as a temp file, then delete afterwards.
Can I know what is the most relevant Upload property in Kendo React which is not allowed to the user to upload the same file again.
Same file been uploaded twice without asking if to replace the file
You can check manually what are in the list of the file upload and then replace that file with the new one or you can tell the user before uploading by checking if same file name already exists in the list if exists then tell the user that the same name file is already exists in the upload list do you want to replace it if yes you can replace it if no then auto rename the latest file uploaded as filename (1) like counter with it.
Hello when ever i try to retrieve the full path of the browsed file by using *
request.getParameter()
***only the file name is being returned to the next jsp file.
Can someone help me..
Thanks in advance
When you upload a file you will get only the File Name and not the entire path that you see in the file upload field. This path is of user file system. If you want to save the file, you need to save it in server side directory path.