LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to JavaScript WRITE file but not read file (https://www.linuxquestions.org/questions/programming-9/how-to-javascript-write-file-but-not-read-file-4175513131/)

fhleung 08-02-2014 09:43 PM

how to JavaScript WRITE file but not read file
 
Dear all,

I would like to WRITE/EDIT a file by Javascript
As far as I know, it could be done by Javascript + php + some XML

If I ONLY use php to parse the file and then WRITE it by server side, what is the difference from above? like pros and cons?

The reason I want to WRITE/EDIT a file by Javascript just
because I have NO networking/internet!
so I want to use browser to open the file and then WRITE/EDIT.


https://developer.mozilla.org/en-US/.../API/FileEntry

graemef 08-03-2014 12:27 AM

Using Javascript to write to a file is not the easiest solution. Allowing a language to write to the filesystem via the browser is considered to be a security risk and so Javascript, the language, doesn't permit this. It is possible to get Javascript to write to a file which can be downloaded but I would suggest that alternative languages would be much better suited to your requirements.

NevemTeve 08-03-2014 03:23 AM

> The reason I want to WRITE/EDIT a file by Javascript just because I have NO networking/internet!

You mean you want to edit local files with Apache+PHP+own_PHP_program+browser+own_JS_program? notepad.exe would be a better solution.

dugan 08-03-2014 10:56 AM

The HTML5 filesystem API is extremely limited for obvious security reasons. You can't just store files anywhere you want, for example.

fhleung 08-03-2014 09:21 PM

"notepad.exe would be a better solution."
I know notepad.exe can edit the file I need, I can also open the file by browser like:
File -> Open Ctrl+O -> browse


Now most web applications have 3 parts which is APACHE web server + PHP script language + XX SQL database (date file)
SQL database/data file can be read/write through the web browser interface, it seems sort of secure... That's the way they doing,
I mean the data file NOT read/write directly by notepad.exe but indirectly by web browser



"Allowing a language to write to the filesystem via the browser is considered to be a security risk"
I can open the core data file/databaseIf open and write the filesystem/file directly... that means I can open the core data file/database directly, I'd think it better using browser to write

fhleung 08-03-2014 09:34 PM

"It is possible to get Javascript to write to a file which can be downloaded but I would suggest that alternative languages would be much better suited to your requirements."

So again, do you mean Javascript CAN write to a file??

astrogeek 08-03-2014 10:41 PM

Quote:

Originally Posted by fhleung (Post 5214417)
"It is possible to get Javascript to write to a file which can be downloaded but I would suggest that alternative languages would be much better suited to your requirements."

So again, do you mean Javascript CAN write to a file??

No.

From the page linked in the original post, please read...:

Quote:

Non-standard
This feature is non-standard and is not on a standards track. Do not use it on production sites facing the Web: it will not work for every user. There may also be large incompatibilities between implementations and the behavior may change in the future.

The FileEntry interface of the File System API represents a file in a file system. It lets you write content to a file.
...

This specification is more or less abandoned as it didn't get significant traction among browser makers.


Quote:

Originally Posted by fhleung (Post 5213969)
The reason I want to WRITE/EDIT a file by Javascript just
because I have NO networking/internet!

This makes no sense, so I wonder if you have some other reason for wanting to write to a file from javascript?

NevemTeve 08-03-2014 11:27 PM

@OP: Do you realize that JavaScrcipt and PHP are entirely different things?
1. PHP runs on the server; JavaScript, on the client.
2. PHP is able to use both databases and files; JavaScript is unable (or with serious restrictions)

fhleung 08-03-2014 11:59 PM

Yes, PHP is server side script language and Javascript need only browser to run.

No, Javascript cannot connect to XX SQL database,
but javascript can read other file type.

graemef 08-04-2014 12:02 AM

Javascript can be used to write to a temporary file which you can then "download". These files don't get written to the file system and so they don't get "saved" in the normal sense. This is for security reasons, if the language allowed you to write to the users filesystem then writing viruses in Javascript would be easier and the virus payload could become more destructive.

Your description of a web application is not complete. You need to consider where the various parts are. Crudely, the database and scripting language are on the server whilst the browser and Javascript are on the user's computer. So it is fine to get the scripting language, or even the Javascript to ask questions of the database because the database is on the server and that is the purpose of the database, whilst the user would be surprise if after viewing a web site the files on their computer had changed.

astrogeek 08-04-2014 12:28 AM

But to point out one more time...

Quote:

Originally Posted by fhleung (Post 5213969)
The reason I want to WRITE/EDIT a file by Javascript just
because I have NO networking/internet!
so I want to use browser to open the file and then WRITE/EDIT.

No networking.

No internet.

So it can only be for use on the client machine where the javascript is running.

So whether or not there is a server running on that machine, it makes no sense at all to try to write a file from javascript - the user ALREADY HAS ACCESS and can use a text editor - unless the OP has left out some important aspect of the intended use.

So please bear with me as I ask once more - what is the actual reason that you want to write from javascript?

dugan 08-04-2014 04:04 PM

Quote:

Originally Posted by fhleung (Post 5214413)
I can open the core data file/databaseIf open and write the filesystem/file directly... that means I can open the core data file/database directly, I'd think it better using browser to write

It is technically not possible to do that from a browser and with only DOM scripting. Deal with it. This should have been clear to you if you read and understood what you linked to in your first post, and if you weren't able to understand what you linked to in your first post then you're not ready to do Javascript development by yourself.

A Java applet would be less restricted in terms of filesystem access, but note that I said "less", not "not".

fhleung 09-04-2014 07:47 AM

I heard a javascript related thing: NODEJS

you think that would work/answer?

dugan 09-04-2014 11:27 AM

Quote:

Originally Posted by fhleung (Post 5232267)
I heard a javascript related thing: NODEJS

you think that would work/answer?

For your offline HTML5 desktop app? I'm going to say yes. Have a look at these:

http://appjs.com/
https://github.com/rogerwang/node-webkit
http://spring.io/blog/2012/10/11/scr...r-from-vmware/

The way it works is that the user has to start up a nodejs server on his or her local machine, and then the browser interacts with that local server that's running in the background.


All times are GMT -5. The time now is 09:03 AM.