会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
Nice感觉
海绵不断的吸收
博客园
首页
新随笔
联系
订阅
管理
将上传文件以二进制形式存入数据库中,并下载数据库中的二进制数据生成对应的文件
以下是读取用户上传的文件,转换成二进制写入数据库:
读取文件转化为二进制写入数据库
byte
[] fileContent
=
new
byte
[
0
];
Stream fileInStream;
int
iSize
=
0
;
iSize
=
FileAttachment.PostedFile.ContentLength;
//
文件大小
if
(iSize
>
1000
*
1024
)
{
lblWrong.Text
=
"
您上传得文件过大,不能超过1000K!
"
;
return
;
}
fileInStream
=
FileAttachment.PostedFile.InputStream;
fileContent
=
new
byte
[iSize];
//
将文件以二进制形式赋值给fileContent
int
iStatus
=
fileInStream.Read(fileContent,
0
, iSize);
以下是读取数据库中的二进制数据转换成对应的文件形式,进行下载:
AttachType 为文件内容类型(MIME)
点下载按钮后进行保存
private
void
UploadBtn_Click(
object
sender, System.EventArgs e)
{
byte
[] tmpAttch
=
(
byte
[])ViewState[
"
BtAttch
"
];
Response.ContentType
=
ViewState[
"
AttachType
"
].ToString().Trim();
//
"application/vnd.ms-excel";
Response.ContentEncoding
=
System.Text.Encoding.Unicode;
string
filename
=
"
CustomDataManager
"
;
Response.AddHeader(
"
Content-Disposition
"
,
"
attachment; filename=
"
+
filename);
this
.Response.Clear();
System.IO.Stream fs
=
this
.Response.OutputStream;
fs.Write(tmpAttch,
0
,tmpAttch.Length);
fs.Close();
this
.Response.End();
}
posted on
2006-06-15 13:03
Gary.han
阅读(
1155
) 评论(
0
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部