MikeTWebb
MikeTWebb

Reputation: 9279

C# Web Application Download File dialog

I have a C# Windows Application and want to open up the standard Save/Download file dialog with a byte[]. I am able to do this in my MVC3 Web App with the below code using System.Web.Mvc.Controller:

FileStream fs =  = System.IO.File.OpenRead(fileName);
File(fs, "application/zip", fileName.Substring(fileName.LastIndexOf('\\') + 1));

How can I accomplish this in the Windows App?

Upvotes: 0

Views: 1595

Answers (1)

Antonio Bakula
Antonio Bakula

Reputation: 20693

Your MVC app didn't open that dialog, browser did. In Winforms application you can use :

Byte[] data;
/// initialize data

using (SaveFileDialog dialog = new SaveFileDialog())
{
  dialog.Filter = "zip files (*.zip)|*.zip|All files (*.*)|*.*";
  dialog.FilterIndex = 2;
  dialog.RestoreDirectory = true;
  if (dialog.ShowDialog() == DialogResult.OK)
  {
    using (FileStream fs = new FileStream(dialog.FileName, FileMode.CreateNew))
    {
      fs.Write(data, 0, data.Length);
    }
  }
}

Upvotes: 1

Related Questions