I want to upload file into folder from which my Angular app is served while running on localhost. I'm not able to find any solution without using backend.
For example I just want to upload an image file and that file should copy in specified folder of the project. This should be done only with Angular without using any Backend script or hitting any API endpoint.
I want to upload file into folder from which my Angular app is served while running on localhost. I'm not able to find any solution without using backend.
For example I just want to upload an image file and that file should copy in specified folder of the project. This should be done only with Angular without using any Backend script or hitting any API endpoint.
Share Improve this question edited Aug 22, 2018 at 9:13 Tin 4345 silver badges13 bronze badges asked Jul 12, 2018 at 15:27 Abdul RafayAbdul Rafay 3,3915 gold badges26 silver badges54 bronze badges 8- Can you clarify a bit more exactly what it is you're trying to do? You can access basically any file from your assets directory. What type of file are you wanting to "upload" and what do you want to do with it? Is it just a .json file, excel spreadsheet, csv, etc..? – Narm Commented Jul 12, 2018 at 15:32
- for example I just want to upload an image file and that file should copy in specified folder of project. This should be done only with Angular without using any backend script or hitting any api endpoint. – Abdul Rafay Commented Jul 12, 2018 at 15:37
- There is a good opensource package for doing this, check out: stackoverflow./questions/48889783/… – Narm Commented Jul 12, 2018 at 15:59
- 1 The TeraData Covalent library's FileUpload ponent still expects to hit an API. Check the "Usage" section of: teradata.github.io/covalent/#/ponents/file-upload – Brandon Taylor Commented Jul 12, 2018 at 17:15
- 1 You might try a service worker approach. angular.io/guide/service-worker-getting-started . It allows to manage a local cache – Piero Commented Aug 20, 2018 at 17:29
4 Answers
Reset to default 3 +50Depending on your webhost, you can make your assets-folder accessible via FTP. Making a FTP-call from javascript (angular is javascript) isn't that difficult. And there are plenty of example and questions about it on the internet (like this)
Why you wouldn't do that:
- The credentials for your ftp-connection will be accessible in the piled javascript-code. With a little bit of effort, everyone can find it.
- Each gate you open through the webhosts firewall, is a extra vulnerability. Thats why everybody will remend you to add an API endpoint for uploading files so that you keep holding the strings of what may be uploaded.
Edit:
As I read your question again and all the sub-answers, I (think) figured out that you are building an native-like app with no back-end, just an angular-single page front-end application. An I can understand why (you can run this on every platform in an application that supports javascript), but the problem you are encountering is only the first of a whole series.
If this is the case, I wouldn't call it uploading
as you would store it locally.
But the good news is that you have localstorage
for your use to store temporary data on the HDD of the client. It isn't a very large space but it is something...
The assets folder is one of the statically served folders of the Angular app. It is located on the server so you can't add files to it without hitting the server (HTTP server, API, or whatever else...).
Even when running your app on localhost, there's a web server under the hood, so it behaves exactly the same than a deployed application, and you can't add files to the assets folder via the Angular app.
I don't know what exactly you want to do with your uploaded files, but:
- If you want to use them on client side only, and in one user session, then you can just store the file in a javascript variable and do what you want with it
- If you want to share them across users, or across user sessions, then you need to store them on the server, and you can't bypass an API or some HTTP server configuration
Based on your clarification in one of your ments:
I'm trying to develop a small speed test application in which user can upload any file from his system to check upload and download speed.
The only way to avoid having you own backend is to use 3rd party API.
There are some dedicated speed test websites, which also provide API access. E.g.:
- https://myspeed.today
- http://www.speedtest
- https://speedof.me/api.html
- Some more: https://duckduckgo./?q=free+speedtest+api
Note, that many of these APIs are paid services.
Also, I've been able to find this library https://github./ddsol/speedtest, which might indicate that speedtest has some kind of free API tier. But this is up to you to investigate.
This question might also be of help, as it shows using speedtest in React Native: Using speedtest api with React Native
You can use a third party library such ng-speed-test. For instance here is an Angular library which has an image hosted on a third party server (ie GitHub) to test internet speed.