blob表
id int
name char
blob image
type char
saveFile
aspx
cs
PRivate void Button
_Click(object sender
System
EventArgs e)
{
Stream imgdatastream = File
PostedFile
InputStream;
int imgdatalen = File
PostedFile
ContentLength;
string imgtype = File
PostedFile
ContentType;
string name = this
getFileNameByURL(this
File
PostedFile
FileName)
byte[] imgdata = new byte[imgdatalen];
int n = imgdatastream
Read(imgdata
imgdatalen)
string connstr =
workstation id=OVERMIND;packet size=
;user id=sa;passWord=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj
;
SqlConnection connection = new SqlConnection(connstr)
SqlCommand command = new SqlCommand(
INSERT INTO blob(name
type
blob) VALUES ( @imgtitle
@type
@blob )
connection )
SqlParameter paramTitle = new SqlParameter(
@imgtitle
SqlDbType
VarChar
)
paramTitle
Value = name;
command
Parameters
Add(paramTitle)
SqlParameter paramData = new SqlParameter(
@blob
SqlDbType
Image )
paramData
Value = imgdata;
command
Parameters
Add( paramData )
SqlParameter paramType = new SqlParameter(
@type
SqlDbType
VarChar
)
paramType
Value = imgtype;
command
Parameters
Add( paramType )
wztj
debug
TestSQL
TraceErrorSql(
INSERT INTO blob(name
type
blob) VALUES ( @imgtitle
@type
@blob )
command
Parameters)
connection
Open()
int numRowsAffected = command
ExecuteNonQuery()
connection
Close()
}
listFile
aspx//這個東西主要用來列表
把已經有的東西列出來
<asp:HyperLinkColumn DataNavigateUrlField=
id
HeaderText=
產品名稱
DataNavigateUrlFormatString=
/getFile
aspx?ID={
}
DataTextField=
name
DataTextFormatString=
{
}
ItemStyle
HorizontalAlign=
Center
ItemStyle
Width=
px
>
listFile
aspx
cs
string connstr=
workstation id=OVERMIND;packet size=
;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj
;
SqlConnection connection = new SqlConnection(connstr)
SqlCommand command = new SqlCommand(
select * from blob
connection )
connection
Open()
SqlDataAdapter adaptor = new SqlDataAdapter(command)
DataSet ds = new DataSet()
adaptor
Fill(ds
blob
)
connection
Close()
this
DataGrid
DataSource=ds
Tables[
blob
]
DefaultView;
this
DataGrid
DataBind()
getFile
aspx
cs//這個文件比較重要負責把村道數據庫裡面的文件
按照格式
按照名稱
給傳輸出來
private void Page_Load(object sender
System
EventArgs e)
{
string imgid =this
Request
QueryString
Get(
ID
)
//Request
QueryString[
imgid
];
string connstr=
workstation id=OVERMIND;packet size=
;user id=sa;password=sa;data source=OVERMIND;persist security info=False;initial catalog=wztj
;
string sql=
SELECT name
blob
type FROM blob WHERE id =
+ imgid;
SqlConnection connection = new SqlConnection(connstr)
SqlCommand command = new SqlCommand(sql
connection)
connection
Open()
SqlDataReader dr = command
ExecuteReader()
if(dr
Read())
{
Response
Clear()
Response
Buffer= true;
Response
Charset=
GB
;
Response
ContentEncoding=System
Text
Encoding
GetEncoding(
GB
)
//設置輸出流為簡體中文
//Response
ContentType =
application/ms
word
;//設置輸出文件類型為word文件
Response
ContentType = dr[
type
]
ToString()
Response
BinaryWrite( (byte[]) dr[
blob
] )
string FileName = dr[
name
]
ToString()
Trim()
FileName=System
Web
HttpUtility
UrlEncode(FileName
System
Text
Encoding
UTF
)
Response
AppendHeader(
Content
Disposition
attachment;filename=
+FileName)
}
connection
Close()
}
這裡要說的有兩點
第一
就是把文件的名稱getFile
aspx變成我們想要的名稱
Response
AppendHeader(
Content
Disposition
attachment;filename=
+FileName)
第二
就是把指定的名稱變成我們想要的值
是標准的中文
而不是中文的亂碼
FileName=System
Web
HttpUtility
UrlEncode(FileName
System
Text
Encoding
UTF
)
From:http://tw.wingwit.com/Article/program/net/201311/12630.html