2013-08-01 185 views
0

实际上,我已经在Sql中保存了一个BinaryData中的文件,现在我可以通过将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驱动器。 现在,我的要求是,我需要得到一个提示保存该文件,用户需要选择保存位置。 这可能吗?

回答

2

您创建一个固定的位置,这里FILESTREAM:

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; 
+0

它给出这样的错误: 在进行OLE调用之前,当前线程必须设置为单线程单元(STA)模式。确保您的Main函数具有标记的STAThreadAttribute。只有在调试器连接到进程时才会引发此异常。 – RealSteel

+0

@RealSteel Justs在主方法中添加'[STAThread]'注释,请参阅http://stackoverflow.com/questions/6373645/c-sharp-winforms-how-to-set-main-function-stathreadattribute以获取更多信息 – derape

0

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

0

有可能,

您可以使用您在设计师创建saveFileDialog并称之为“秀”的方法来打开对话框:

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

0

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

如果它是一个ASP.NET MVC应用程序,则可以直接返回FileContentResult作为操作结果。否则,您可以使用下面的示例调整到你的代码

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

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