file download in react application - javascript

I am trying to understand download facility in reactjs. actually first I am uploading one file using reactjs. so for that I have given file upload path and it will return one url in return in the database. so for ex if I am uploading one file it is returning one url.
in my env file I have given this location UPLOAD_LOCATION="C:\Users\SS2345\Desktop\shruti" .as of now I am giving location of my local machine.
in database for this file a path will be saved.
for ex "localhost:3000/api/4f3ce528-7c99-4516-9ccf-95d8d0be74a2.docx" file name has been generated due to some algorithm and my api is running on localhost:3000/api path. file is located on this path. C:\Users\SS2345\Desktop\shruti
how can I use it to download. I am writing below.
<div >
<a href="http://localhost:3000/api/certificates/uploads/8373f87f-9a28-4c1b-a80b-8038fe990e96.docx" download>File Download</a>
</div>
I am getting error.
?
what step I need to do to achieve it?

Related

How to store data with nodejs outside of the project folder?

I am using ExpressJs and i want to save my files outside of the project like this.
im using this code for upload and its ok
my files uploaded correctly but i can not show them in the app cause route will be like this
http://localhost:8000/../../fileArchive/1661839542935/name.jpg
nut when i set this to src of an image, the src doesnt show my image
This is because express is trying to protect your project files from getting exposed on a malicious request. By default you cant access any of the files in your project or other directories. You can define a directory that can be accessed using the express.static method.
This will define a directory as a static directory which can be accessed (see express.static). After you added this you can also drop the any path in your URL. Express will check for any files matching the name in one of your static directories.
// This would allow the user to access the files in that directory
// ../fileArchive will be considered as the fileRoot e.g. localhost:8000/test.txt would look for a test.txt file in ../fileArchive
app.use(express.static('../fileArchive'));
// if you want to keep fileArchive as part of the url you can do that like so
// however I would recommend to exclude the `..` to keep the url readable and also the exact file path on the server should not concern the user just where he can find it
app.use('/fileArchive', express.static('../fileArchive'))

replace complete HTML file content from javascript

I have a html file under my war folder in the project ,
I just want to access that file From the path "/myhtml.html"
and then replace whatever is in this file from a new text "Some new Text"
so that My html file now will only have "some new text" there .
I searched alot for this , but they provide example for changing a specific div etc in a file , but i just want to replace everything in a file which is present under a war folder.
I understand that you are trying to change the content of HTML files using Javascript that is running in the browser.
Front-end Javascript can't access local files that are stored on your machine. It would be a security disaster if it could. The only way Javascript can read with local files is using a file upload input, and it will only be able to read the files not update/overwrite them.
NodeJS however is capable of accessing the local file system. NodeJs is backend JavaScript so it will be running on a server.

Getting File Path

I recently made a Python app where I take an Excel file, convert it to a CSV file, and then shoot it out with an Email. I tried turning this application into a Web App and here is where issues arose. In order to send the a file using Email, I need to get the full file path and I am not sure how to get it. In order to let the user pick out the file, I used <input type="file">, but this only supplies me with the name of the file and not the actual file path. Any suggests on what I should do to fix this issue?
Use the os module in python.
import os
os.path.abspath('/path/to/file')

How to select destination for output AngularJS/Node.js

I have an application built with AngularJS and node.js that takes in a csv file, does some work on it and then outputs it.
Currently the only way I can output it is:
<a download="fileName.csv" ng-href="{{file}}">fileName.csv</a>"
But this will always put it in the 'Downloads' folder. Is there a way to ask for the destination and use that to output the file?
AFAIK There's no way to pick where the file ends up on the client machine, that's determined by their browser. It's going to your downloads folder because that's the default location.

how to get pdfJS to render a pdf from URL?

i am using PDFJS to render PDFs files using their URL after scanning the current page a js snipet return the urls. then it passes them to pdfJS. until now everything works the problem show when the PDF is already open in the browser . i take the URL (*.pdf) and pass it the same way as before the difference is that the file is not downloaded and i have this response.
Warning: Unhandled rejection: Unexpected server response (0) while retrieving PDF "http://geekographie.maieul.net/IMG/pdf/progit.fr.pdf".
(just for the record i dont have CORS issues).
Try this "http://mozilla.github.io/pdf.js/"
Installation
download and unzip file.
put "PDF" file in this folder.
config in "viwer.js", edit var DEFAULT_URL.
upload folder to server.
Have fun!

Categories

Resources