tags:

views:

177

answers:

2

This is my Django Project structure:

Project
 -app_ABC
    -json_data
      -filter_saved.txt
 -site_media
    -css
    -images
    -scripts
       -search.js

In My script (search.js) ->I am going to create a script that can write content to file as json format:

function WriteJSONFile(str){
    var fh = fopen("json_data/filter_saved.txt", 3);//Open the file for writing
     // If the file has been successfully opened
    if(fh!=-1) {
        fwrite(fh, str); // Write the string to a file
        fclose(fh); // Close the file
    }
 }  

$(document).ready(function(){
    $("#save_filter").click(function(){
         var filter_saved = []
         var customer_type_selected = $('#id_customer_type :selected').text();
         var list_tag_selected = $("#tag_checked").val();
         var filter_name = $("#put_filter_name").val();
         filter_saved_JSON = {
                         "pk":autoincrement,
                         "customer_type": customer_type_selected,
                         "tag": list_tag_selected
                        };
          WriteJSONFile(filter_saved_JSON);

    });

});

My problems is

 var fh = fopen("json_data/filter_saved.txt", 3);//Open the file for writing
 Where "filter_saved.txt" Could I put into directory?

I tried to place in many directories in project (I am using ubuntu)

I got an error:

fopen is not defined
[Break on this error] var fh = fopen("json_data/filter_saved.txt", 3);//Open the file for writing\n

Could anyone help me ?

Thanks :)

+1  A: 

What the hell are you trying to do? Javascript runs in the browser, on the client side. It has no filesystem hooks, and thank god for that. Why on earth would you think it could open and write to files on your server?

ironfroggy
Maybe you dont understand my question or my writing? :)
python
You are trying to open a file descriptor to a file located on your server from javascript, which is running on the client. Is that not correct?
ironfroggy
http://www.c-point.com/JavaScript/articles/file_access_with_JavaScript.htm just to be like this..
python
My case is Read and write content to file.this in common question that I am seeing now http://www.brighthub.com/internet/web-development/articles/24628.aspxBTW Thank ironfroggy for your replied.:)
python
The link you showed is running local javascript as a batch/shell script, not as part of a webpage for end-users. You simply cannot do this. This is part of what javascript is. besides, its running on a completely different machine when your user pulls up the page. Do you think its safe to let your users write to files on your server?
ironfroggy
Also, look into some ajax. Your javascript can make a request back to the Django code, which can do the writing for you, on the server.
ironfroggy
Thanks Ironfroggy ,this is What I want to know...:merry Christmas And a happy New Year. ...
python
+1  A: 

You cannot access the file system on the client side using javascript. If you are trying to write to a file on your sever (what it looks like your doing), then this needs to be in the sever side code (python in this case).

aubreyrhodes
Thanks aubreyrhodes ! this is the one that I should know http://www.brighthub.com/internet/web-development/articles/24628.aspx
python