0

Actually, I have saved a file in BinaryData in Sql and now I am able to download that file by converting the BinaryData into Bytes.

My code is :

object value = (sender as DevExpress.Web.ASPxGridView.ASPxGridView).GetRowValues(e.VisibleIndex, "ID");
                    Int64 FileID = Convert.ToInt64(value);

                    var filedata = (from xx in VDC.SURVEY_QUESTION_REPLIES
                                    where xx.ID == FileID
                                    select xx).FirstOrDefault();

                    string fileextension = filedata.FILE_EXTENSION.ToString();
                    string fileName = filedata.ANSWER_TEXT.ToString() + fileextension;

                    string DocumentName = null;
                    FileStream FStream = null;
                    BinaryWriter BWriter = null;
                    byte[] Binary = null;
                    const int ChunkSize = 100;
                    int SizeToWrite = 0;
                    MemoryStream MStream = null;

                    DocumentName = fileName;

                    FStream = new FileStream(@"c:\\" + DocumentName, FileMode.OpenOrCreate, FileAccess.Write);
                    BWriter = new BinaryWriter(FStream);
                    Binary = (filedata.FILE_DATA) as byte[];
                    SizeToWrite = ChunkSize;
                    MStream = new MemoryStream(Binary);

                    for (int i = 0; i < Binary.GetUpperBound(0) - 1; i = i + ChunkSize)
                    {
                        if (i + ChunkSize >= Binary.Length) SizeToWrite = Binary.Length - i;
                        byte[] Chunk = new byte[SizeToWrite];
                        MStream.Read(Chunk, 0, SizeToWrite);
                        BWriter.Write(Chunk);
                        BWriter.Flush();
                    }
                    BWriter.Close();
                    FStream.Close();
                    FStream.Dispose();
                    System.Diagnostics.Process.Start(@"c:\" + DocumentName);

and it is directly saving the file to the location C Drive. Now,My Requirement is that,I need to get a Prompt for saving that file and user need to select the location of saving. Is that Possible ?

RealSteel
  • 1,871
  • 3
  • 37
  • 74

4 Answers4

2

You create a filestream with a fixed location here:

FStream = new FileStream(@"c:\\" + DocumentName, FileMode.OpenOrCreate, FileAccess.Write);

What you would have to do is something like this:

var dialog = new SaveFileDialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
  FStream = new FileStream(dialog.FileName, FileMode.OpenOrCreate, FileAccess.Write);
  // put the rest of your file saving code here
}

Remember to import the Forms namespace

using System.Windows.Forms;
DerApe
  • 3,097
  • 2
  • 35
  • 55
  • Its Giving an error like this : Current thread must be set to single thread apartment (STA) mode before OLE calls can be made. Ensure that your Main function has STAThreadAttribute marked on it. This exception is only raised if a debugger is attached to the process. – RealSteel Aug 01 '13 at 09:39
  • @RealSteel Justs add the `[STAThread]` annotation upon your main method, see http://stackoverflow.com/questions/6373645/c-sharp-winforms-how-to-set-main-function-stathreadattribute for more information – DerApe Aug 01 '13 at 10:44
0

If its Forms app You can use SaveFileDialog class

szpic
  • 4,346
  • 15
  • 54
  • 85
0

it is possible,

you can use a saveFileDialog that you create in your designer and call the "show" method to open that dialog :

private void button1_Click(object sender, System.EventArgs e)
 {
     Stream myStream ;
     SaveFileDialog saveFileDialog1 = new SaveFileDialog();

     saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"  ;
     saveFileDialog1.FilterIndex = 2 ;
     saveFileDialog1.RestoreDirectory = true ;

     if(saveFileDialog1.ShowDialog() == DialogResult.OK)
     {
         if((myStream = saveFileDialog1.OpenFile()) != null)
         {
             // Code to write the stream goes here.
             myStream.Close();
         }
     }
 }

Source : http://msdn.microsoft.com/en-gb/library/system.windows.forms.savefiledialog.aspx

Sebastien H.
  • 6,818
  • 2
  • 28
  • 36
0

I see that you use web components of DevExpress, so assuming that you want to send the stream to response for the client to save the file.

If it is an ASP.NET MVC application, you can return FileContentResult as action result directly. Otherwise, you might use the following sample adapting into your code

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + DocumentName);
MStream.WriteTo(Response.OutputStream);

Depending on user's browser settings, save file dialog might be shown to the user.

nanotech
  • 111
  • 1
  • 3