angular file upload and preview - angularjs

I am using ng-file-upload library to post files to my back end Web Api. Once the file is posted I save it to the following folder
"~/App_Data/Tmp/FileUploads/"
and also save the path to my database.
Now when I go on edit mode; I want to get a preview of all uploaded files (photos). But when I use something like
ng-src="path"
I get
"Not allowed to load local resource"
is this the right approach, when serving files from disk?

That path is a server path and not a client path so for sure that won't work.
You need to have an endpoint on your server to allow downloading those files with id or name/path and then set the src of the img to that url.
The implementation download endpoint depends on your backend technology.
If in your edit mode you still have access to the file object that use has just selected then you can do:
<img ngf-src="file">

Related

How to create folders and images using AngularJS

I have an AngularJS app that, upon requested to upload an image, creates a folder inside the file system (CentOS) and then puts that image there, telling the user everything went OK or not.
Is this possible? All I've found is ways to download the file.
Thanks.
It is better to delegate this task to a service on the server, since it is file that is uploading to a server. I put simple rest service to file upload this file to the server.

Uploading files to a domain using form

I have written an application in CakePHP 3.x and there is a form to upload files.
At present, files are being uploaded to WWW_ROOT.'files' which is /app/webroot/files/ path.
To store files separately from core application, I created a subdomain cdn.example.com whose path is like /home/user/example.com/cdn.example.com/.
since, uploading files requires absolute path, how can I get absolute path of the subdomain cdn.example.com same as $_SERVER['DOCUMENT_ROOT']; from example.com?
First check if your server allow to write files from main domain to subdomain.
You can't get the absolute path of a different domain of the one you deployed your application, you should store the path in a variable or in constant.
If you really need to store files in another domain/subdomain and any path is blocked you should consider to reupload the files via FTP script.

Get JSON distant file and write/store a file with angularjs

I search how to write a file with angularjs, after get it in json. I have already the json file in my folder, but with onclick, users can manually update it with the new content and overwrite the same file. How to write it ? (i don't ask the get distant file part).
I need locastorage functions ? it's for android application, so which storage i must use ?
You can't write files with javascript. You would need to pass the data to PHP or nodejs (aka a server side language) that has access to your filesystem. Javascript is client facing, so it doesn't know anything about the filesystem on the server it is running.

Is it possible to upload a file with malicious filename?

Is it possible to send a http upload request a file to a Apache or IIS that will have a fileName with "../" or ".." that wouldn't be rejected and would be passed to php or ASP.Net engine?
Not really the way you are asking. By the time it gets to the server the browser has read the file and delivered it as a chunk of content with no information about where it came from other than the original file name which you can choose to use or discard.
Generally file uploads go into a temporary storage place (e.g. /tmp) and then need to be moved out of there to somewhere which you can control and name.
This storage is configured on the server, and so any attempt to put path info into the filename should also be blocked by the file upload implementation of the server which should sanitise the filenames again if the browser didn't already do so.
If there's a bug then all bets are off though.

How to download a file from CakePHP?

I'm working on a College Application project.
I want to ask how to download the files that previously uploaded by user.
Here's the clear view :
User will upload the files.
Controller will handle the request and save the name of the files into database.
How admin can download the file ???
I try to access the localhost/system/files/upload.doc but doesn't got any luck.
Consider using Media views: http://book.cakephp.org/1.3/view/1094/Media-Views
These will allow you to go to a path and load the database information about the file and then present the file with necessary download headers to the user.
yes use media views. They are the best way to keep your files from being publically accessible. So an admin can be "Served" the file and others can not get to them .
You can rename the file and things like that as you serve it to the user as well. Very nice feature of cake.

Resources