6 Replies - 1167 Views - Last Post: 03 October 2014 - 10:12 AM

#1 arvi608   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-June 14

Read data from text area to .txt file of specific name

Posted 02 October 2014 - 03:55 PM

Below is the JS i have used and I dont understand where im doing it wrong, please help me out

<script language="javascript">
function WriteToFile()
{
{
var text=document.getElementById("content").innerText;
fh = fopen("/autotmb/home/kpc009/public_html/ICMGB/seqin.txt",3);
fwrite(fh, text);}
fclose(fh); 
}
</script>


 <div class="field">
      <textarea name="content" id="content" placeholder="Type your content here..."></textarea>
    </div>
 <div class="field">
      <button type="submit" onclick="WriteToFile()">Generate Chromatin Folding</button>
      <div id="messages"></div>
        
    </div>


P.S: OS is Linux on my system

This post has been edited by JackOfAllTrades: 03 October 2014 - 03:35 AM
Reason for edit:: Added code tags


Is This A Good Question/Topic? 0
  • +

Replies To: Read data from text area to .txt file of specific name

#2 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,247
  • Joined: 15-January 14

Re: Read data from text area to .txt file of specific name

Posted 02 October 2014 - 04:06 PM

Javascript does not have fopen, fwrite, and fclose functions. In fact, Javascript cannot write to or read from arbitrary files at all.
Was This Post Helpful? 0
  • +
  • -

#3 arvi608   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-June 14

Re: Read data from text area to .txt file of specific name

Posted 02 October 2014 - 04:33 PM

Then please suggest me what would be the best approach for the problem, so that i will guide myself towards it. Thanks

This post has been edited by Dormilich: 04 October 2014 - 12:25 AM

Was This Post Helpful? 0
  • +
  • -

#4 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,247
  • Joined: 15-January 14

Re: Read data from text area to .txt file of specific name

Posted 02 October 2014 - 04:49 PM

If you want to save a file on the user's computer then one option would be to submit the data to the server and use something like PHP to send download headers and the file data. That will cause the browser to show the save/open dialog box, and they can save the file where they want. You can do that with ajax also if you don't want the page to refresh, in that case you would use Javascript to gather the form data and then use Javascript to send the ajax request to a hidden iframe on the page. The user will just see the save/open box without anything else changing on the page. If you don't want to use anything on the server side then you can probably save a file with something like a Java applet, although you might need to ask for special permissions to do that (in addition to requiring that they support Java).
Was This Post Helpful? 0
  • +
  • -

#5 arvi608   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-June 14

Re: Read data from text area to .txt file of specific name

Posted 02 October 2014 - 04:56 PM

Thanks, but what if i want to save the file in a predifined path on my system without save/open dialog box. As in the above code onclick of submit the textarea content should get saved in designated path.


(this is just an application im working to learn, so im not gonna deploy it anywhere-no other users)
Was This Post Helpful? 0
  • +
  • -

#6 Blindman67   User is offline

  • D.I.C Addict
  • member icon

Reputation: 140
  • View blog
  • Posts: 620
  • Joined: 15-March 14

Re: Read data from text area to .txt file of specific name

Posted 02 October 2014 - 05:30 PM

This may help. It just downloads a javascript string as a named txt file into your download directory

To use just call downloadText(filename,text); filename does not need the extension, the browser will append .txt as part of the download. Do not include any path information. text is a Javascript string. Use \n \r \t etc for line feeds and tabs

function downloadText(filename,text) {
    var downloaderLnk = document.createElement('a');  // create a anchor for the download
    var e;
    var blob = new Blob([text], { type: 'text/txt' }); //Create a text Blob
    downloaderLnk.href = URL.createObjectURL(blob);

    // set the filename 
    downloaderLnk.download = filename;           

    // create a fake click event to start the download
    if (document.createEvent) {   
        e = document.createEvent("MouseEvents");
        e.initMouseEvent("click", true, true, window,
                         0, 0, 0, 0, 0, false, false, false,
                         false, 0, null);
        downloaderLnk.dispatchEvent(e);
    } else if (downloaderLnk.fireEvent) {
        downloaderLnk.fireEvent("onclick");
    }
}

Was This Post Helpful? 0
  • +
  • -

#7 ArtificialSoldier   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2039
  • View blog
  • Posts: 6,247
  • Joined: 15-January 14

Re: Read data from text area to .txt file of specific name

Posted 03 October 2014 - 10:12 AM

Quote

Thanks, but what if i want to save the file in a predifined path on my system without save/open dialog box.

I am not aware of any way to do that on the client without possibly something like a Java applet or ActiveX control. You can save files in arbitrary places on the server, but not the client. It's a good thing too. It would be very bad if you could go to a malicious website and have them start saving randomly-named 1kb files all over your hard drive until the space fills up. There are very good reasons why Javascript is not allowed to read or write arbitrary files on your computer.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1