實際上,我已經在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
驅動器。 現在,我的要求是,我需要得到一個提示保存該文件,用戶需要選擇保存位置。 這可能嗎?
它給出這樣的錯誤: 在進行OLE調用之前,當前線程必須設置爲單線程單元(STA)模式。確保您的Main函數具有標記的STAThreadAttribute。只有在調試器連接到進程時纔會引發此異常。 – RealSteel
@RealSteel Justs在主方法中添加'[STAThread]'註釋,請參閱http://stackoverflow.com/questions/6373645/c-sharp-winforms-how-to-set-main-function-stathreadattribute以獲取更多信息 – derape