ASP.NET的FileUpload控件可用于上傳文件到服務器。HoverTreeTop新增了一個“閱圖”功能,圖片就是用FileUpload上傳的。
這里要說明的是上傳圖片限定文件名和文件大小等代碼。
文件上傳功能使用用戶控件實現,在HoverTreePanel項目中的HTPanel\HControl\UCPictureAdd.ascx 控件,
HoverTreeTop上傳的圖片文件暫時限定為jpg、png和gif。代碼為:
1
|
<asp:FileUpload runat= "server" ID= "fileUpload_hovertree" ClientIDMode= "Static" accept= "image/png,image/jpeg,image/gif" /> |
c#代碼:
1
2
3
4
5
6
7
|
HtPictureInfo h_info = new HtPictureInfo(); h_info.HtSuffix = HoverTreeImageTool.GetGpjImageFileExtension(fileUpload_hovertree.PostedFile.ContentType); if (h_info.HtSuffix == "" ) { literal_tips.Text = "請選擇jpg,png或者gif圖片文件" ; return ; } |
其中GetGpjImageFileExtension方法在HoverTreeFrame項目中,代碼:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
namespace HoverTree.HoverTreeFrame.HtImage { public class HoverTreeImageTool { /// <summary> /// 根據圖片文件的mime內容類型獲取文件的后綴名,如果不是gif,png或者jpg圖片文件則返回空字符串 /// </summary> /// <param name="contentType"></param> /// <returns></returns> public static string GetGpjImageFileExtension( string contentType) { switch (contentType) { case "image/jpeg" : return "jpg" ; case "image/pjpeg" : return "jpg" ; case "image/gif" : return "gif" ; case "image/png" : return "png" ; case "image/x-png" : return "png" ; default : return string .Empty; } } } } |
也就是使用ContentType獲取并驗證后綴名。參考:http://hovertree.com/texiao/h/contenttype/
還有一個就是限定上傳文件的大小,暫時限定為1M,代碼如下:
1
2
3
4
5
|
if (fileUpload_hovertree.PostedFile.ContentLength > 1048576) { literal_tips.Text = "選擇的文件太大。" ; return ; } |
1048576字節也就是1M。
上傳使用SaveAs方法就可以:
fileUpload_hovertree.SaveAs(h_fullName);
其中h_fullName為完整文件名字符串。
源碼下載:
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://hovertree.com/h/bjag/o5rdqyrb.htm