2

When using browseForSave method, is it possible to let the user choose only the location of a file, and not the file name or the extension?

I'm creating an encrypted file, and I need its name and extensions not to be changed by the user.

Thx!


EDIT (AFTER SOLVED)

I was simply looking for the browseForDirectory method. Shame on me. :)

Sr.Richie
  • 5,680
  • 5
  • 38
  • 62
  • I don't believe restricting this is possible, in general I believe the thought is this is something of a security issue since this might allow some way to tell the user your saving A while you're really saving to B (this is just a guess that they didn't want the potential for a larger surface for security breaches, in AIR these restrictions are lifted assuming the person installed your program then they trust you as a source. I believe you'll just have to make the fact that the name cannot change crystal clear to your users. – shaunhusain Sep 01 '12 at 18:22
  • thx @shaunhusain, hope that's not true.... The file need to be read by another application, and I want the other app to look for a specific file – Sr.Richie Sep 01 '12 at 18:31
  • You are building this as a web application correct? if it's an AIR app you can simply use File and FileStream to save directly where you need to. – shaunhusain Sep 01 '12 at 18:48
  • No, it's an AIR app (i'm going to edit the title right now). The fact is that I do need the user to select the destination directory – Sr.Richie Sep 01 '12 at 18:53
  • Ah okay then I think you're in business http://stackoverflow.com/questions/5001457/how-to-create-browse-for-folder-dialog-in-adobe-flex – shaunhusain Sep 01 '12 at 18:54
  • thx again, but I'm already using browseForSave. The problem is that using that method doesn't let you to "lock" file name and extension :) – Sr.Richie Sep 01 '12 at 18:57
  • can't you simply pull the directory chosen from it on select then make your own new File object with "basePathChosen" then you append your "locked" file name and extension? are you using f.browseForDirectory("Choose a directory"); – shaunhusain Sep 01 '12 at 18:59

2 Answers2

1

For a reference on how to open a browse dialog to choose a folder see the example here:

How to create "Browse for folder" dialog in Adobe FLEX?

once you have your directory you can piece that together with code here to save a file using the FileStream object:

http://blog.everythingflex.com/2008/02/25/file-and-filestream-within-air/

copied here since it's an external link

    private function saveFile():void{
        var myPattern:RegExp = / /g;
        var newFileName:String = fileName.text.replace('.txt','');
        if(newFileName.length > 1){
            var file:File = File.desktopDirectory.resolvePath("Files/" + newFileName.replace(myPattern,'_') + ".txt");
            var stream:FileStream = new FileStream()
            stream.open(file, FileMode.WRITE);
            var str:String = contents.text;
            str = str.replace(/\r/g, File.lineEnding);
            stream.writeUTFBytes(str);
            stream.close();
            fdg.directory = File.desktopDirectory.resolvePath("Files/");
            fileName.text = "";
            contents.text = "";
          } else {
              mx.controls.Alert.show("File name is required", "Error Saving File");
          }
    }
Community
  • 1
  • 1
shaunhusain
  • 19,630
  • 4
  • 38
  • 51
  • OMG. Shame on me. I completely missed the browseForDirectory method!!!! That was what I was looking for. Thx for you help mate – Sr.Richie Sep 01 '12 at 19:33
  • @Sr.Richie sure no problem, figured you may have overlooked that since they're similar names I think it was an issue for me at first too. – shaunhusain Sep 02 '12 at 00:33
0

One solution would be to set the file object like this:

var f:File = File.desktopDirectory.resolvePath("*.txt");
f.addEventListener(Event.SELECT, onSelected);
f.browseForSave("save txt file");

Although user can still override that in the opened dialog, but at least the dialog shows the file extension. And later in the Event.SELECT you can check and confirm what user has selected.

Hadi tavakoli
  • 1,267
  • 2
  • 16
  • 30