最近在Q群里,有幾個人問到的文件上傳,大多數是問,如何判斷,如何獲取文件大小之類的,本文首先對FileUpload控件作出解析,
一般文件上傳頁面都會作為一個獨立的頁面處理,因為需要修改from標記的傳輸方式,尤其是使用FileUpload里的PostedFile屬性的時候,必須要加上enctype="multipart/form-data".否則不能正確的獲取到PostedFile屬性,
而PostedFile屬性里,封裝了幾個很有用的屬性,分別是:ContentType、ContentLength、FileName、InputStream.
ContentType 主要獲取文件的類型,而不是文件的后聚名,在一定程度上,可以起來類型判定的作用,
ContentLength 獲取文件大小。
FileName 文件名字。
InputStream 以流方式輸入。
這個InputStream其實是個很有用的東西,很多人想問,如果我要在上傳的時候修改圖片大小,幫圖片加水印,這些加工工序,InputStream可以幫到你在上傳文件的同時,作出上操作
下面是我一個例子的代碼貼,主要作用是上傳圖片
ASPX頁面
?
<
table?
width
="340px"
?height
="180px"
?style
="margin:?0px;?padding:?0px"
>
????????????
<
tr
>
????????????????
<
td?
class
="td3"
?width
="20px"
>
????????????????????
<
asp:FileUpload?
ID
="PhotoFileUpload"
?runat
="server"
?
/>
????????????????????
<
asp:Button?
ID
="UpImageBtn"
?runat
="server"
?Text
="上傳圖片"
?OnClick
="UpImageBtn_Click"
?
/>
????????????????
</
td
>
????????????
</
tr
>
????????????
<
tr
>
????????????????
<
td?
class
="td4"
?height
="140px"
?valign
="top"
>
????????????????????
<
ul?
style
="color:?mediumslateblue;?text-align:?left"
>
????????????????????????????
<
li
>
本系統只支持200K以內的圖片
</
li
>
????????????????????????????
<
li
>
只支持JPG、GIF、PNG格式
</
li
>
????????????????????????
</
ul
>
????????????????????
<
asp:Panel?
ID
="UpLoadedPanel"
?runat
="server"
?Visible
="false"
>
????????????????????????
<
ul?
style
="color:?Red;?text-align:?left"
>
????????????????????????????
<
li
>
文件位置:
<
asp:Literal?
ID
="FilePlaceLi"
?runat
="server"
></
asp:Literal
></
li
>
????????????????????????????
<
li
>
文件類型:
<
asp:Literal?
ID
="FileTypeLi"
?runat
="server"
></
asp:Literal
></
li
>
????????????????????????????
<
li
>
文件大?。?/span>
<
asp:Literal?
ID
="FileLenghtLi"
?runat
="server"
></
asp:Literal
>
KB
</
li
>
????????????????????????
</
ul
>
????????????????????
</
asp:Panel
>
????????????????
</
td
>
????????????
</
tr
>
????????????
<
tr
>
????????????????
<
td?
class
="td3"
>
????????????????????
<
asp:Label?
ID
="MessageBoxLab"
?runat
="server"
></
asp:Label
>
????????????????????
<
input?
type
="button"
?id
="ClossWindows"
?value
="完成"
?onclick
="ThisOk()"
?
/>
????????????????
</
td
>
????????????
</
tr
>
????????
</
table
>
?
CS代碼:
?1
protected
?
void
?UpImageBtn_Click(
object
?sender,?EventArgs?e)
?2
????
{
?3
????????
if
?(Page.IsValid)
?4
????????
{
?5
????????????
if
?(PhotoFileUpload.HasFile)
?6
????????????
{
?7
????????????????UpLoadedPanel.Visible?
=
?
true
;
?8
????????????????FilePlaceLi.Text?
=
?PhotoFileUpload.PostedFile.FileName;
?9
????????????????FileLenghtLi.Text?
=
?(PhotoFileUpload.PostedFile.ContentLength?
/
?
1024
).ToString();
10
????????????????FileTypeLi.Text?
=
?PhotoFileUpload.PostedFile.ContentType;
11
????????????????
string
?fileName?
=
?PhotoFileUpload.FileName;
12
????????????????
string
?strExPrentFile?
=
?fileName.Substring(fileName.LastIndexOf(
"
.
"
)?
+
?
1
);
13
????????????????
string
?strFileType?
=
?PhotoFileUpload.PostedFile.ContentType;
14
????????????????
string
[]?upExPrentFile?
=
?
new
?
string
[]?
{?
"
image/pjpeg
"
,?
"
image/gif
"
,?
"
image/x-png
"
}
;
15
????????????????
bool
?IsUp?
=
?
false
;
16
????????????????
for
?(
int
?i?
=
?
0
;?i?
<
?upExPrentFile.Length;?i
++
)
17
????????????????
{
18
????????????????????
if
?(strFileType.Trim().ToLower().Equals(upExPrentFile[i].ToLower()))
19
????????????????????
{
20
????????????????????????IsUp?
=
?
true
;
21
????????????????????}
22
????????????????}
23
????????????????
if
?(IsUp)
24
????????????????
{
25
????????????????????
string
?SavePath?
=
?
string
.Format(
"
../Photo/{0}.{1}
"
,?DateTime.Now.ToString(
"
mmhhddss
"
),?strExPrentFile);
26
27
28
????????????????????
if
?((PhotoFileUpload.PostedFile.ContentLength?
/
?
1024
)?
<
?
200
)
29
????????????????????
{
30
????????????????????????
object
?imag?
=
?PhotoFileUpload.PostedFile.InputStream;
31
????????????????????????ImageSize?mm?
=
?
new
?ImageSize();
//
32
????????????????????????
string
?oo?
=
?
""
;
33
????????????????????????mm.SaveThreePicture(imag,?Server.MapPath(SavePath),?
out
?oo);
34
35
????????????????????????MessageBoxLab.Text?
=
?
"
上傳成功
"
;
36
????????????????????????HiddenText.Value?
=
?oo;
37
????????????????????}
38
????????????????????
else
39
????????????????????
{
40
????????????????????????MessageBoxLab.Text?
=
?
"
文件大小不允許超過200K
"
;
41
????????????????????}
42
????????????????}
43
????????????????
else
44
????????????????
{
45
????????????????????MessageBoxLab.Text?
=
?
"
文件類型不正確
"
;
46
????????????????}
47
????????????}
48
????????}
49
????}
50
posted on 2009-05-09 11:29
^喬喬^ 閱讀(1348)
評論(2) 編輯 收藏 引用 所屬分類:
c#