Jamie Fearon
Jamie Fearon

Reputation: 2634

Is Uploading a JSON file to the server necessary?

I am building a GAE app that basically lets a user choose a JSON file on there computer, this JSON file will then be changed a little (i.e a few fields inserted) and uploaded to the server for storage in a database.

I am altering the JSON file locally using Javascript.

My question is, since you cannot load and change JSON (and other resources) from a local machine does this mean I have to firstly upload the unaltered JSON to the server?

i.e. do I have to do this:

  1. User selects raw unaltered JSON file from their computer
  2. This is then uploaded to the server
  3. The unaltered JSON is then sent back to the client
  4. Perform some javascript wizardry on the JSON to add the new fields etc I want.
  5. Upload the altered JSON file to the database
  6. Delete the old unaltered JSON file from the database

Would this be the correct method? It seems quite long-winded.

Thanks for your help people :)

Upvotes: 4

Views: 1578

Answers (1)

Jeff-Meadows
Jeff-Meadows

Reputation: 2184

If the user's browser doesn't support the File API, then I think your steps may be the best solution. However, many modern browsers do support the API, so you may be able to make use of it.

The gist is to create a new FileReader object and load it. When that's done, you should be able to evaluate the JSON (using JSON.parse), made your edits, and then do your uploading.

Read more here: http://www.html5rocks.com/en/tutorials/file/dndfiles/

Here's a demo using images: http://html5demos.com/file-api

Of course, if it's possible to do the processing server-side instead of client-side, the situation becomes fairly trivial as well.

Upvotes: 3

Related Questions