2013-08-01 183 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); 

根據用戶的瀏覽器設置,保存文件對話框會顯示給用戶。