提示将文件保存到磁盘

时间:2013-08-01 08:49:43

标签: c# .net file binary-data

实际上,我已将BinaryData中的文件保存在Sql中,现在我可以通过将BinaryData转换为Bytes来下载该文件。

我的代码是:

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);

并将文件直接保存到C驱动器位置。 现在,我的要求是,我需要获得保存该文件的提示,用户需要选择保存位置。 这可能吗?

4 个答案:

答案 0 :(得分:2)

您可以在此处创建具有固定位置的文件流:

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

你需要做的是这样的事情:

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
}

请记住导入Forms命名空间

using System.Windows.Forms;

答案 1 :(得分:0)

如果其表单应用程序您可以使用SaveFileDialog类

答案 2 :(得分:0)

有可能,

您可以使用在设计器中创建的saveFileDialog并调用“show”方法打开该对话框:

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();
         }
     }
 }

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

答案 3 :(得分:0)

我看到您使用DevExpress的Web组件,因此假设您要将流发送到响应以便客户端保存文件。

如果它是ASP.NET MVC应用程序,则可以直接将FileContentResult作为操作结果返回。否则,您可以使用以下示例适应您的代码

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

根据用户的浏览器设置,可能会向用户显示保存文件对话框。