0

I have a function in the controller that downloads a configurations:

public FileStreamResult SaveData()
{
    var toJson = JsonConvert.SerializeObject(this.GetData());
    var byteArray = System.Text.Encoding.ASCII.GetBytes(toJson);
    var stream = new MemoryStream(byteArray);
    string fileName = "Configuration.json";
    this.GetData().Save = false;
    return new FileStreamResult(stream, "text/html")
    {
        FileDownloadName = fName
    };
}

It works fine, but the only issue is that it downloads file automatically. I want to provide a user with a possibility to update the name and set the download location, i.e. I want a "save as" dialog to popup prior to download.

I looked multiple sources, but cannot find something, which will be applicable. Can anyone suggest, how it should be done?

Thanks in advance.

tolik
  • 187
  • 1
  • 10
  • 4
    Sounds more like a browser preference setting, which you won't be able to control from your code. For example, in Chrome I believe the default is automatic downloads, and you (as a user) can change the setting to prompt for save as. So I guess the question is: what browsers have you tried this on? Have you tried multiple browsers? – musefan May 22 '18 at 12:32
  • Totally agree with @musefan regarding this. I've looked at it some time ago and unfortunately the Save As dialogue is a browser implementation over which you have no control. – Andrei U May 22 '18 at 12:38
  • Haven't tried this, but might help.. https://stackoverflow.com/questions/6816727/save-file-dialog-in-mvc – Leron May 22 '18 at 12:39
  • @musefan, thanks. I tried via Chrome and Mozilla, it opens a popup, where it asks whether to save or open, but without a possibility to change the filename – tolik May 22 '18 at 12:39

1 Answers1

0

Different browsers behave differently regarding the displaying of a Save As dialog. The only way to have any control is to not give the browser cues as to the data type or name of the file. In these cases browsers have no choice but to ask the user for a filename.

Replace "text/html" with "application/octet-stream". this says "here is a bunch of bytes, but I don't know what they represent".

Remove the FileDownloadName. If the browser doesn't have a name, it will need to ask for one.

You may need to do one or both of the above, depends on the browsers you care about.

Philip Smith
  • 2,741
  • 25
  • 32