Save a file using client-side javascript


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: Save a file using client-side javascript

  1. #1
    Anjana Guest

    Save a file using client-side javascript


    Hi,
    I want to load a file,make changes in it and save it using client side javascript.Any
    ideas/ suggetsions will be greatly helpful for me.

  2. #2
    Dev Ashish Guest

    Re: Save a file using client-side javascript

    You could try some client side Automation with VBScript. Although, IMHO,
    this sounds too close to being a virus type app. I would suggest asking the
    user to submit the file so that you can change it server-side and
    response.binarywrite it back.

    -- Dev

    "Anjana" <tmmet@hotmail.com> wrote in message
    news:38e8eec7$1@news.devx.com...
    :
    : Hi,
    : I want to load a file,make changes in it and save it using client side
    javascript.Any
    : ideas/ suggetsions will be greatly helpful for me.



  3. #3
    paul larmuseau Guest

    Re: Save a file using client-side javascript


    Hi, i want to keep in touch to solve this problem


    i read you search a solution for the same problem

    i guess or it's something made impossible consiously

    http://chart.yahoo.com/table.csv?s=3...1&amp;c=3D199=
    5&amp;d=3D2&amp;e=3D2&amp;f=3D1999&amp;g=3Dm&amp;q=3Dq&amp;y=3D0&amp;z=3D=
    nite&amp;x=3D.csv

    i want to read these data csv data in a string

    i tried the same thing as you did, but discovered the same errors

    http://www.inquiry.com/techtips/js_p...ro&docID=3D37=
    56

    i read already something should go with HTMLscript ?

    I read also it should go with java applets

    Q & A - Reading from URL into String
    =20

    Applies to These Versions Categories =20
    Version Numbers Not Applicable General
    =20

    -------------------------------------------------------------------------=
    -
    What is the maximum size of a string array I can have in my =
    applet? I am trying to store some stock prices in a string array after =
    reading them from a URL connection, but I get an applet exception. What =
    is going wrong? Any suggestions?

    I suspect you're trying to read the contents of the URL =
    using URLConnection.getContent() and that this is not working for you. =
    Netscape's implementation of getContent() is known to have bugs when =
    it's dealing with simple text files. A better way to read the data into =
    your applet is to use InputStreams.=20

    For example, if your stock data were stored in a text file =
    called "stock.txt" on the Web server and you wanted your applet to read =
    the contents of "stock.txt" into a string using a URL, you would do the =
    following:

    String inputLine;
    String stockData =3D "";

    URL stockURL =3D new URL(getDocumentBase(), "stock.txt"));
    DataInputStream s =3D new DataInputStream(stockURL.openStream());

    while ((inputLine =3D s.readLine()) !=3D null) {
    stockData =3D stockData + inputLine;
    }

    s.close();
    =20
    This will read the contents of "stock.txt" into a single =
    String object and allocate memory for it as needed. The String will grow
    =
    to accommodate the amount of data in the data file. You can then use =
    StringTokenizer to read the data out of the String and parse it as you =
    see fit.=20

    Written by inquiry.com Staff on 3/20/97.
    =20
    =20



    or we should pass through notepad again with an applet ?

    Q & A - Copy & Paste
    =20

    Applies to These Versions Categories =20
    Version Numbers Not Applicable AWT
    =20

    -------------------------------------------------------------------------=
    -
    How can I copy the selected text from a TextArea to the =
    system clipboard or paste from the system clipboard to a TextArea?=20

    Java 1.1 introduced the java.awt.datatransfer package, which
    =
    implements the basic mechanisms for using the system clipboard. It =
    defines a Clipboard class through which you can cut and paste arbitrary =
    data. To access the system clipboard, you first need to get a reference =
    to the default system toolkit with Toolkit.getDefaultToolkit(). Then you
    =
    can retrieve a Clipboard instance representing the system clipboard by =
    calling getSystemClipboard(). Now that you have a reference to the =
    system clipboard, all you have to do to implement a copy operation is to
    =
    fetch the selected text from the TextArea, and set the clipboard =
    contents. TextArea selections are accessed with getSelectedText() and =
    Clipboard contents are set with setContents(). As you can see, a lot of =
    Java programming just involves knowing which methods to call and what =
    they do. Pasting from the clipboard involves retrieving its contents =
    with getContents() and inserting the text into the TextArea at the =
    current cursor position. The accompanying program demonstrates how to =
    perform these operations.=20

    Of special note is that not all window systems posses the =
    same notion of a system clipboard. The X Window system only maintains =
    the concept of a current selection, without requiring a cut and paste =
    operation. The instant a piece of text is selected, it becomes eligible =
    for pasting. Windows 95 and the Mac require you to cut or copy a =
    selection before it can be pasted. The system clipboard example program =
    will only function as expected in a cut and paste window environment. In
    =
    the X Window system, a Java program using native peer widgets like =
    java.awt.TextArea, will automatically handle selection copying through =
    the native window system, and the techniques in the example become =
    nonapplicable.=20


    import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;

    public final class ClipboardExample extends Frame {
    private TextArea __textArea;
    private Button __copyButton, __pasteButton;
    private Clipboard __systemClipboard;

    public ClipboardExample() {
    super("Clipboard Demo");
    ActionListener copyListener, pasteListener;

    __systemClipboard =3D =
    Toolkit.getDefaultToolkit().getSystemClipboard();

    __textArea =3D new TextArea("Start typing!");
    __textArea.setColumns(30);
    __textArea.setRows(20);
    __textArea.setEditable(true);

    __copyButton =3D new Button("Copy");
    __pasteButton =3D new Button("Paste");

    copyListener =3D new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    String selection;
    StringSelection stringSelection;

    selection =3D __textArea.getSelectedText();
    if(selection =3D=3D null)
    selection =3D "";

    stringSelection =3D new StringSelection(selection);
    __systemClipboard.setContents(stringSelection, stringSelection);
    }
    };

    pasteListener =3D new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Transferable contents;
    String selection;

    contents =3D __systemClipboard.getContents(null);
    try {
    selection =3D=20
    (String)contents.getTransferData(DataFlavor.stringFlavor);
    System.out.println("FOO");
    System.out.println(selection);
    __textArea.insert(selection, __textArea.getCaretPosition());
    } catch(UnsupportedFlavorException ufe) {
    ufe.printStackTrace();
    } catch(java.io.IOException ioe) {
    ioe.printStackTrace();
    }
    }
    };

    __copyButton.addActionListener(copyListener);
    __pasteButton.addActionListener(pasteListener);

    setLayout(new BorderLayout());
    add(__textArea, BorderLayout.CENTER);
    add(__copyButton, BorderLayout.NORTH);
    add(__pasteButton, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
    ClipboardExample example;
    WindowListener exitListener;

    exitListener =3D new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    Window window =3D e.getWindow();
    window.setVisible(false);
    window.dispose();
    System.exit(0);
    }
    };

    example =3D new ClipboardExample();
    example.addWindowListener(exitListener);
    example.pack();
    example.setVisible(true);
    }
    }


    Written by Daniel Savarese on 9/15/98.
    =20
    =20


    ------=_NextPart_001_000A_01BFA312.EF83BBF0
    Content-Type: text/html;
    charset="iso-8859-1"
    Content-Transfer-Encoding: quoted-printable

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD>
    <META content=3D"text/html; charset=3Diso-8859-1" =
    http-equiv=3DContent-Type>
    <META content=3D"MSHTML 5.00.2516.1900" name=3DGENERATOR>
    <STYLE></STYLE>
    </HEAD>
    <BODY bgColor=3D#ffffff>
    <DIV><FONT face=3DArial size=3D2>Hi, i want to keep in touch to solve =
    this=20
    problem</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>i read you search a solution for the =
    same=20
    problem</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>i guess or it's something made =
    impossible=20
    consiously</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><A=20
    href=3D"http://chart.yahoo.com/table.csv?s=3Dintc&amp;amp;a=3D1&amp;amp;b=
    =3D1&amp;amp;c=3D1995&amp;amp;d=3D2&amp;amp;e=3D2&amp;amp;f=3D1999&amp;am=
    p;g=3Dm&amp;amp;q=3Dq&amp;amp;y=3D0&amp;amp;z=3Dnite&amp;amp;x=3D.csv">ht=
    tp://chart.yahoo.com/table.csv?s=3Dintc&amp;amp;a=3D1&amp;amp;b=3D1&amp;a=
    mp;c=3D1995&amp;amp;d=3D2&amp;amp;e=3D2&amp;amp;f=3D1999&amp;amp;g=3Dm&am=
    p;amp;q=3Dq&amp;amp;y=3D0&amp;amp;z=3Dnite&amp;amp;x=3D.csv</A></FONT></D=
    IV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>i want to read these data csv data in a
    =

    string</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>i tried the same thing as you did, but =
    discovered=20
    the same errors</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2><A=20
    href=3D"http://www.inquiry.com/techtips/js_pro/answer.asp?pro=3Djs_pro&am=
    p;docID=3D3756">http://www.inquiry.com/techtips/js_p...r.asp?pro=3Dj=
    s_pro&amp;docID=3D3756</A></FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>i read already something should go with
    =
    HTMLscript=20
    ?</FONT></DIV>
    <DIV><FONT face=3DArial size=3D2></FONT>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>I read also it should go with java=20
    applets</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>
    <TABLE border=3D0 cellPadding=3D0 cellSpacing=3D0 width=3D600>
    <TBODY>
    <TR>
    <TD rowSpan=3D10 width=3D10></TD>
    <TD vAlign=3Dtop><!-- Answer ASP Code -->
    <TABLE>
    <TBODY>
    <TR>
    <TD vAlign=3Dtop><FONT color=3D#000000 face=3DVerdana=20
    size=3D4><B>Q&nbsp;&amp;&nbsp;A - Reading from URL into=20
    String</B></FONT><BR><IMG border=3D0 height=3D2=20
    src=3D"cid:000501bfa302$2bf34ad0$08147bd4@BEHEERDER"=20
    width=3D300></TD></TR></TBODY></TABLE><BR>
    <TABLE border=3D0 cellPadding=3D2 cellSpacing=3D2 width=3D450>
    <TBODY>
    <TR>
    <TH><FONT face=3DVerdana size=3D2>Applies to These =
    Versions</FONT> </TH>
    <TH><FONT face=3DVerdana size=3D2>Categories</FONT> </TH></TR>
    <TR>
    <TD vAlign=3Dtop width=3D200>
    <CENTER><I>Version Numbers Not Applicable</I> </CENTER></TD>
    <TD vAlign=3Dtop width=3D200><FONT size=3D1><A=20
    =
    href=3D"http://www.inquiry.com/techtips/java_pro/browse.asp?pro=3Djava_pr=
    o&amp;catID=3D484"><IMG=20
    border=3D0 hspace=3D5 =
    src=3D"cid:000601bfa302$2bf4d170$08147bd4@BEHEERDER"=20
    vspace=3D1>General</A><BR></FONT></TD></TR></TBODY></TABLE>
    <HR align=3Dleft color=3D#000000 SIZE=3D1 width=3D450>

    <TABLE width=3D450>
    <TBODY>
    <TR>
    <TD><FONT size=3D2>
    <P><B>What is the maximum size of a string array I can have =
    in my=20
    applet? I am trying to store some stock prices in a string =
    array=20
    after reading them from a URL connection, but I get an =
    applet=20
    exception. What is going wrong? Any suggestions?</B></P>
    <P>I suspect you're trying to read the contents of the URL =
    using=20
    <CODE>URLConnection.getContent()</CODE> and that this is not
    =
    working=20
    for you. Netscape's implementation of =
    <CODE>getContent()</CODE> is=20
    known to have bugs when it's dealing with simple text files.
    =
    A=20
    better way to read the data into your applet is to use=20
    <CODE>InputStreams</CODE>.=20
    <P>For example, if your stock data were stored in a text =
    file called=20
    "stock.txt" on the Web server and you wanted your applet to =
    read the=20
    contents of "stock.txt" into a string using a URL, you would
    =
    do the=20
    following:<PRE> String inputLine;
    String stockData =3D "";

    URL stockURL =3D new URL(getDocumentBase(), "stock.txt"));
    DataInputStream s =3D new DataInputStream(stockURL.openStream());

    while ((inputLine =3D s.readLine()) !=3D null) {
    stockData =3D stockData + inputLine;
    }

    s.close();
    </PRE>This will read the contents of "stock.txt" into a single=20
    String object and allocate memory for it as needed. The =
    String will=20
    grow to accommodate the amount of data in the data file. You
    =
    can=20
    then use StringTokenizer to read the data out of the String =
    and=20
    parse it as you see fit.=20
    <P></P>Written by inquiry.com Staff on=20
    =
    3/20/97.</I><BR></FONT></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABL=
    E></FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>or we should pass through notepad again
    =
    with an=20
    applet ?</FONT></DIV>
    <DIV>&nbsp;</DIV>
    <DIV><FONT face=3DArial size=3D2>
    <TABLE border=3D0 cellPadding=3D0 cellSpacing=3D0 width=3D600>
    <TBODY>
    <TR>
    <TD rowSpan=3D10 width=3D10>&nbsp;</TD>
    <TD vAlign=3Dtop><!-- Answer ASP Code -->
    <TABLE>
    <TBODY>
    <TR>
    <TD vAlign=3Dtop><FONT color=3D#000000 face=3DVerdana=20
    size=3D4><B>Q&nbsp;&amp;&nbsp;A - Copy &amp; =
    Paste</B></FONT><BR><IMG=20
    border=3D0 height=3D2 =
    src=3D"cid:000701bfa302$2bf4d170$08147bd4@BEHEERDER"=20
    width=3D300></TD></TR></TBODY></TABLE><BR>
    <TABLE border=3D0 cellPadding=3D2 cellSpacing=3D2 width=3D450>
    <TBODY>
    <TR>
    <TH><FONT face=3DVerdana size=3D2>Applies to These =
    Versions</FONT> </TH>
    <TH><FONT face=3DVerdana size=3D2>Categories</FONT> </TH></TR>
    <TR>
    <TD vAlign=3Dtop width=3D200>
    <CENTER><I>Version Numbers Not Applicable</I> </CENTER></TD>
    <TD vAlign=3Dtop width=3D200><FONT size=3D1><A=20
    =
    href=3D"http://www.inquiry.com/techtips/java_pro/browse.asp?pro=3Djava_pr=
    o&amp;catID=3D194"><IMG=20
    border=3D0 hspace=3D5 =
    src=3D"cid:000801bfa302$2bf65810$08147bd4@BEHEERDER"=20
    vspace=3D1>AWT</A><BR></FONT></TD></TR></TBODY></TABLE>
    <HR align=3Dleft color=3D#000000 SIZE=3D1 width=3D450>

    <TABLE width=3D450>
    <TBODY>
    <TR>
    <TD><FONT size=3D2>
    <P><B>How can I copy the selected text from a TextArea to =
    the system=20
    clipboard or paste from the system clipboard to a TextArea? =
    </B></P>
    <P>Java 1.1 introduced the java.awt.datatransfer package, =
    which=20
    implements the basic mechanisms for using the system =
    clipboard. It=20
    defines a Clipboard class through which you can cut and =
    paste=20
    arbitrary data. To access the system clipboard, you first =
    need to=20
    get a reference to the default system toolkit with=20
    Toolkit.getDefaultToolkit(). Then you can retrieve a =
    Clipboard=20
    instance representing the system clipboard by calling=20
    getSystemClipboard(). Now that you have a reference to the =
    system=20
    clipboard, all you have to do to implement a copy operation =
    is to=20
    fetch the selected text from the TextArea, and set the =
    clipboard=20
    contents. TextArea selections are accessed with =
    getSelectedText()=20
    and Clipboard contents are set with setContents(). As you =
    can see, a=20
    lot of Java programming just involves knowing which methods =
    to call=20
    and what they do. Pasting from the clipboard involves =
    retrieving its=20
    contents with getContents() and inserting the text into the =
    TextArea=20
    at the current cursor position. The accompanying program=20
    demonstrates how to perform these operations.=20
    <P>Of special note is that not all window systems posses the
    =
    same=20
    notion of a system clipboard. The X Window system only =
    maintains the=20
    concept of a current selection, without requiring a cut and =
    paste=20
    operation. The instant a piece of text is selected, it =
    becomes=20
    eligible for pasting. Windows 95 and the Mac require you to =
    cut or=20
    copy a selection before it can be pasted. The system =
    clipboard=20
    example program will only function as expected in a cut and =
    paste=20
    window environment. In the X Window system, a Java program =
    using=20
    native peer widgets like java.awt.TextArea, will =
    automatically=20
    handle selection copying through the native window system, =
    and the=20
    techniques in the example become nonapplicable.=20
    <P><PRE>import java.awt.*;
    import java.awt.event.*;
    import java.awt.datatransfer.*;

    public final class ClipboardExample extends Frame {
    private TextArea __textArea;
    private Button __copyButton, __pasteButton;
    private Clipboard __systemClipboard;

    public ClipboardExample() {
    super("Clipboard Demo");
    ActionListener copyListener, pasteListener;

    __systemClipboard =3D =
    Toolkit.getDefaultToolkit().getSystemClipboard();

    __textArea =3D new TextArea("Start typing!");
    __textArea.setColumns(30);
    __textArea.setRows(20);
    __textArea.setEditable(true);

    __copyButton =3D new Button("Copy");
    __pasteButton =3D new Button("Paste");

    copyListener =3D new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    String selection;
    StringSelection stringSelection;

    selection =3D __textArea.getSelectedText();
    if(selection =3D=3D null)
    selection =3D "";

    stringSelection =3D new StringSelection(selection);
    __systemClipboard.setContents(stringSelection, stringSelection);
    }
    };

    pasteListener =3D new ActionListener() {
    public void actionPerformed(ActionEvent event) {
    Transferable contents;
    String selection;

    contents =3D __systemClipboard.getContents(null);
    try {
    selection =3D=20
    (String)contents.getTransferData(DataFlavor.stringFlavor);
    System.out.println("FOO");
    System.out.println(selection);
    __textArea.insert(selection, __textArea.getCaretPosition());
    } catch(UnsupportedFlavorException ufe) {
    ufe.printStackTrace();
    } catch(java.io.IOException ioe) {
    ioe.printStackTrace();
    }
    }
    };

    __copyButton.addActionListener(copyListener);
    __pasteButton.addActionListener(pasteListener);

    setLayout(new BorderLayout());
    add(__textArea, BorderLayout.CENTER);
    add(__copyButton, BorderLayout.NORTH);
    add(__pasteButton, BorderLayout.SOUTH);
    }

    public static void main(String[] args) {
    ClipboardExample example;
    WindowListener exitListener;

    exitListener =3D new WindowAdapter() {
    public void windowClosing(WindowEvent e) {
    Window window =3D e.getWindow();
    window.setVisible(false);
    window.dispose();
    System.exit(0);
    }
    };

    example =3D new ClipboardExample();
    example.addWindowListener(exitListener);
    example.pack();
    example.setVisible(true);
    }
    }
    </PRE>


  4. #4
    Join Date
    Apr 2006
    Posts
    1

    File Validation using Javascript

    I want javascript code using which i can validate whether selected file is present at client side or not.

    I want to check whether file existing or not before posting form to my PHP file.

    I want a code which can run on both window and Linux.

    Thanx

  5. #5
    Join Date
    May 2008
    Posts
    1

    Opening a notepad text file in Javascript

    Hi,
    I want to load a open a notepad text file in Javascript,make changes in it and save it using client side javascript.Any
    ideas/ suggetsions will be greatly helpful for me.

  6. #6
    Join Date
    Mar 2007
    Location
    Bangalore, India
    Posts
    247
    I guess my reply will be a disappointment. But it is not possible for client side JavaScript to access the file system in a web browser. It is a security breach, and will not be permitted. If such a possibility is found, it might be a browser bug and could be fixed in the future releases.

    In fact, you can't even validate whether a file is present or not using JavaScript. This kind of things will have to be done at server side.

    However, the good news is, signing a script gives you some of these privileges. For more details on signing a script, please refer http://www.mozilla.org/projects/secu...d-scripts.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center