实际上,我已经在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