Sony Shakya
Sony Shakya

Reputation: 21

Creating an external .txt in as3

how do I automatically create an external .txt file without asking the user for directory. I am using the following code so as to create and save the ".txt "file, but my code asks for the directory to save the file. I want the file to be saved automatically without asking user for the directory...

import flash.net.FileReference;
import flash.events.Event;


addEventListener(Event.ENTER_FRAME,saveFile);
var ss:String = "this is text to write";
var fileRef:FileReference;
function saveFile(event:Event):void
{
    fileRef=new FileReference();
    fileRef.save(ss,"save.txt");

    removeEventListener(Event.ENTER_FRAME, saveFile);
}

Upvotes: 2

Views: 8102

Answers (5)

Vishwas
Vishwas

Reputation: 1541

Can be done if you compile to AIR (Desktop app). But cannot be done with browser. As i think, it will go against browser's security. Just think, if it were possible, then websites would start storing and loading content on your system without your permission. :)

Upvotes: 0

Mark Robbins
Mark Robbins

Reputation: 2457

If you are running in a browser, you CAN save to a file, you have to use external interface and be on file:/// protocol and use something like jQuery.twFile.js

Upvotes: 0

tszarzynski
tszarzynski

Reputation: 614

You cannot do that for security reasons if it's a browser app... Maybe you can try using SharedObjects instead...

Upvotes: 2

pho
pho

Reputation: 25489

Are you creating an AIR appplication for desktop? If so, you can use File and FileStream.

var f:File=new File("path\to\file.txt");
var str:FileStream=new FileStream();
str.open(f, FileMode.WRITE);
str.writeUTFBytes(contents);
str.close();

If you are creating a flash-player application, then you must ask the user for a path to save (because an online app saving without a user's knowledge is just WRONG)

Upvotes: 5

Philipp Kyeck
Philipp Kyeck

Reputation: 18820

is definitely a security issue - saving files without the user knowing what you're doing just isn't right. AIR is the exception - as a "normal" appliaction it can save a file without asking where to put it.

Upvotes: 1

Related Questions