Skip navigation
Currently Being Moderated

Is it possible?

Jun 24, 2012 2:12 PM

Hi everyone,

im a beginer with flash pro so please be gently

I got a project in mind and I wanna know if it is possible or not.

I have 4 label in my project.

First Label got 2 text box (Date.text and Name.text)

Second label got 2 text box too( Adress.text and Reason.text)

Third label got 3 text box (Calltime.text, Arrivedtime.text and Leavingtime.text)

Fourth label got 1 text box called «Details.text»

I also have a Button named Savebutton (Viewable on he four label).

 

What I want is, When I press on the «Savebutton», I want flash to create a new .txt document in a certain folder on my computer. I also want it to name that document with the First textbox of my project (in this case, «Date.text») but also put this textbox in the «.txt» document.

 

So, when i'll open my «23june2012.txt» file, I'll see something like that:

 

Date:23june2012

Name:Bill
Adress:8765 Old street
Reason:Car accident
Calltime:17:05

Arrivedtime:17:50

Leavingtime:20:37

Details:A blue honda Civic 2010  ''364 VCG'' crashed on a Red Mazda 3 ''321 JHK''.

 

So I juste wanna know if we can create this kind of things with Flash - Actionscript 3.

Thanks a lot

 

 

Bambi

 
Replies
  • Currently Being Moderated
    Jun 26, 2012 2:43 AM   in reply to Bambi Rock

    You can try the below code. It works for me....

     

    import flash.events.MouseEvent;
    import flash.net.FileFilter;
    import flash.filesystem.File;
    import flash.filesystem.FileStream;
    import flash.filesystem.FileMode;

     

    Save.addEventListener(MouseEvent.CLICK,saveDetails);


    function saveDetails(e:MouseEvent):void
    {
    var file:File = File.applicationStorageDirectory.resolvePath(Date.text.replace(/^\s+| \s+$/g, "")+".txt");
    var stream:FileStream = new FileStream();
    stream.open(file, FileMode.WRITE);
    var str:String = "Date:"+Date.text +"\n"+ "Name:"+Name.text +"\n"+ "Address:"+Address.text +"\n"+ "Reason:"+Reason.text +"\n"+ "Calltime:"+Calltime.text +"\n"+ "Arrivedtime:"+Arrivedtime.text +"\n"+ "Leavingtime:"+Leavingtime.text +"\n"+ "Details:"+Details.text;
    trace(str.replace("\\n","\n"));
    stream.writeUTFBytes(str);
    stream.close();
    }

     

     

    I took the textfields names as you mentioned. (.txt file is saved in local store of the program file. Program file location will be dependent on the storage directory you gave.)

     

    Here I took applicationStorageDirectory for storing a file. You can store your file in different directories. For that once you check File class in adobe. You can get the total logic. And one more thing is when using File in your program you should set the publish settings to air.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 9:08 PM   in reply to Bambi Rock

    Yes You have to create some user interface for my code.

     

    First create some labels and textfields as follows

    Untitled.png

     

    Go to stage and click on the text tool. In properties select the type as static text and make all those labels as in the above screen. Now again select text tool and select input text as the type and give instance name for each text field as I mentioned in the code (for example, for the textfield beside the Date label should be named as Date like that)  and under character properties, select show border around text. Then take button component and drag it to the stage. Give the instance name as Save and in properties change the label as Save. Now your user interface is completed.

     

     

    I used Date.text in code means Date is the instance name of the textfield and Date.text is the text you entered in the textbox. Text fields names should be matched with the code.

     
    |
    Mark as:
  • Currently Being Moderated
    Jun 26, 2012 11:20 PM   in reply to bhargavi reddy

    You can also use FileReference to save the file in your system.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 1, 2012 9:26 PM   in reply to Bambi Rock

    You should select "Use Device Fonts" property from anti-alias dropdown list which is under Character in properties of textbox. So that you will be able to enter all chars.

     

    Using FileReference you can put .txt file in your mentioned location. Use save method for FileReference instance, once search it in google (filereference in as3). You can understand how to use it.

     

    Use FileReference instead of FileStream as follows:

     

    import flash.events.MouseEvent;
    import flash.net.FileFilter;
    import flash.filesystem.File;
    import flash.net.FileReference;

     

    Save.addEventListener(MouseEvent.CLICK,saveDetails);


    function saveDetails(e:MouseEvent):void
    {
    var file:FileReference = new FileReference();


    var str:String = "Date:"+Date.text +"\n"+ "Name:"+Name.text +"\n"+ "Address:"+Address.text +"\n"+ "Reason:"+Reason.text +"\n"+ "Calltime:"+Calltime.text +"\n"+ "Arrivedtime:"+Arrivedtime.text +"\n"+ "Leavingtime:"+Leavingtime.text +"\n"+ "Details:"+Details.text;
    str.replace("\\n","\n");

     

    file.save(str,Date.text.replace(/^\s+| \s+$/g, "")+".txt");
    }

     

    I am not practically check this one. First understand FileReference and try this one. If there are any mistakes in my code you can find those easily.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 4, 2012 9:15 PM   in reply to Bambi Rock

    file.save(str,Date.text.replace(/^\s+| \s+$/g, "")+".txt");

     

    I will explain this line.

     

     

    str is the data that you want to save in a file. Date is the instance name of the date textfield. Date.text is the text that you given in the date text field i.e., date. You want to save your file with the naem of date right? So that I took the text given in the datefield to set the title for file. First check whether you give the instance name of particular textfield as Date or not. If so change that one.

     

    Here I used replace function to trim the right and left side spaces in the string i.e, in date. And I concat .txt extension for saving the file as txt file.

     

    If you give the date as 3july2012. The above line should be

     

    file.save(str,3july2012.txt);

     

    As per the syntax 1st argument is the data and 2nd argument is the filename.

     

    Simply I am using string functions.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 2:20 PM   in reply to Bambi Rock

    file.save(str,Date.text.replace(/^\s+| \s+$/g, "")+".txt");

     

    If it's not working, and giving you that odd file name with function in it, you might try breaking out that inner code to remove it from inside the save method like so:

     

    var fName:String = Date.text.replace(/^\s+| \s+$/g, "")+".txt";

    file.save(str, fName);

     



     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 10:32 PM   in reply to Bambi Rock

    I also checked it. But no use. I too get the same error. I didn't find the solution. I hope i will be back with solution or atleast help.

     
    |
    Mark as:
  • Currently Being Moderated
    Jul 5, 2012 11:10 PM   in reply to bhargavi reddy

    Once check behaviour property under paragraph of textfield. It must be single line. All are working fine but it doesn't detect \n. I am unable to find the reason. For that I put "," instead of "\n" in str.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points