0

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

John Stimac
  • 5,335
  • 8
  • 40
  • 60
chris
  • 36,115
  • 52
  • 143
  • 252
  • 2
    You'll have to use AJAX (however you do it - jQuery, vanilla JS, etc), and a little bit of server side scripting; the file won't write itself. – Bojangles Aug 19 '11 at 20:51
  • I know through Server Side like PHP or ASP or something to the effect there of (other than ActiveX) I can do it, hell I wouldn't be asking here if it were that simple. I have a demand however to avoid server side script if and at all where ever possible. So that said I need to know through and through 100% that this is not possible via JavaScript alone before I am allowed to say ok I have to do it this way in regards to the PHP or similar. – chris Aug 19 '11 at 20:54
  • 1
    With only javascript this is not possible. This question has been asked many times before: http://stackoverflow.com/questions/371875/local-file-access-with-javascript – Luwe Aug 19 '11 at 20:54
  • I think I have my answer over all though now. Prior to asking I was coming up with mixed results via bing google other the community here is much more reliable so I figured my last ditch effort would be to ask a question i was 99% sure about the answer here so I can confidently say 100% instead. Thank you everyone.. – chris Aug 19 '11 at 20:57

5 Answers5

2

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.

Pekka
  • 442,112
  • 142
  • 972
  • 1,088
1

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

John Stimac
  • 5,335
  • 8
  • 40
  • 60
1

No. Javascript runs on the client. You need server-side code to access the server's file system.

Diodeus - James MacFarlane
  • 112,730
  • 33
  • 157
  • 176
1

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.

zzzzBov
  • 174,988
  • 54
  • 320
  • 367
1

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.

Blazemonger
  • 90,923
  • 26
  • 142
  • 180