How to read file client-side without using a file input? - javascript

I'm struggling to know how to read a file (client-side) in JS without using a file input, as I only need to read a local file in my project.
What I want to achieve is to get the content from a .less file in order to put it into a parser of some kind (I need to access its content in a javascript file).
I cannot use "fs" as I'm not using node, but I don't seem to be able to use the FileReader API either as it needs a Blob object which would be obtained by the file input.
Does someone have an idea ?

client side doesn't have access to manage file system, you can read file with input but cannot write or delete from client side. it is only possible with a backend application written in node js or java, from their you can expose those services and call it from client side through http requests.

Related

storing info as a JSON file on a server

So I have information i need to save. I am using a JavaScript object then I store the info in a JSON file.
But I want to put my project on a server and run it. but currently I am using fs to read and write data.
how would i do that on a server
You can have the JSON file in the server and read/write to it the same way you are doing locally, it makes no difference.
That said, I'd recommend using something like http://npmjs.com/package/json-server to make it work like an actual server API.

Can I create a directory in client PC?

I have multiple questions
Can we create a directory in client machine ?
Can we check the running browser's default download path?
My requirement is that the user will download a BLOB data from my DB that time I want to create a folder in client machine and use that folder to save the blob data.
Can this be done using javascript,jquery,ajax,php,angularjs ?
You can't create a folder in client PC using JS.(don't even bother looking with HTML and CSS)
The best way you could accomplish something as you describe is to give a .zip to your client, so while extracting it, it creates his own container (the folder). But you must be sure that the people who intend using your web application knows how a .zip works.
Using PHP you can't do anything in the client PC. PHP is a server side programming language. So, it is running only in the server. When you make any request to the server then only PHP performs. In the browser end or client side only HTML, CSS and JS works.

Writing Data into CSV File located at particular path on Client Side

The application which am working is an static application which is built on Angular JS technology. I want to write data into CSV File located at particular path so that another API can read data from that path(CSV File). I had searched a lot in internet but i could not find correct solution. Could you please help me regarding this. Technology can be any client side like Angular JS,Jquery,Java Script.
You can't do that without an server like node, apache (php), ... to save the data.
-> Saving a text file on server using JavaScript

Difference between reading files client-side and server-side

What exactly is the difference between accessing a file through HTML (e.g. <img src="xxx.jpg">) or JavaScript, and using PHP to read a file. The file is still just on the server in both cases, isn't it?
What makes using PHP to access the server's file system different to someone just typing in the URL of the file (provided they know what it is)?
javascript is client side and runs in the browser.
php is server side and runs in the server.
note that there are now server side javascript interpreters like node.js or phantom.js
For static files like (images, HTML, etc) there is no difference, if you directly enter the file URI or read it internally and print the buffer through PHP (at this case the file URI will change for sure).
For dynamic files (PHP files), the same concept is true, if you enter the file URI directly you will get the output of that file, and if you read it internally (There are two types: include and read its source). but in most cases, PHP files are designed for execution not for printing the source, therefore, in both cases:
When you try to read static files by PHP its just resource wasting if there is no need to use PHP.
In the two cases you mentioned the file is on the server. If the file is static such as images, you can have its URL directly in the HTML.
In some cases you need to to have a dynamic URL for the content, for example a download service that generates a temporary URL for the content after authenticating the user. In such case you would serve the file using PHP since you do not have a fixed URL for the content
Like you are saying, php might really access the FILE SYSTEM, while a client just can access files the Server handles to them.

With JavaScript is it possible to Read/Write from/to a file on the server

I have a series of JSON Objects I want to save locally on my server. I am attempting to avoid any server-side script like PHP as required per demand of whats being built. I know its a security risk, but that in this case is not a particular worry. So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
I should mention I am attempting to avoid ActiveX as I know this is an IE only feature and the software we are developing is planned to be Cross Browser supported
So that said is it possible to actually write to a file and or get its contents via javascript with or without the help of a lib such as jquery?
Nope. You will need something running on server side that can receive your JavaScript input and write it to the server.
Internet Explorer's proprietary file writing functionality is for writing local (client-side) files only.
You can read a file using ajax, but without a server side language you cannot write a file to the server.
https://developer.mozilla.org/en/ajax
No. Javascript runs on the client. You need server-side code to access the server's file system.
Client-side JavaScript can only send data to a server, there's no way for it to tell the server what to do with the data.
To save data to a file or db on a server, you'll require a server-side script of some sort (could be server-side JS with Node.js). If all you need is persistent data, you could store some JSON strings in localStorage or in cookies as needed. They wouldn't be shareable that way though.
Yes, you can use AJAX requests in JavaScript without using jQuery. However, jQuery will save you an ungodly amount of time and cross-browser testing.
But, as others have already said, you can't write server files without server code.

Categories

Resources