0

实际上,我已经BinaryData在 Sql 中保存了一个文件,现在我可以通过将其转换BinaryDataBytes.

我的代码是:

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

4 回答 4

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;
于 2013-08-01T08:59:54.493 回答
0

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

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

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

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

于 2013-08-01T09:19:45.167 回答
0

如果它的 Forms 应用程序您可以使用 SaveFileDialog 类

于 2013-08-01T09:00:17.667 回答
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

于 2013-08-01T09:00:18.197 回答