读取文件转化为二进制写入数据库
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);
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();
}
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();
}