国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > .NET > 正文

通過.NET上傳圖象

2024-07-10 13:12:12
字體:
來源:轉載
供稿:網友

以前,通過asp上傳圖象(圖象的大小、類型都受到限制)一般都是要借助外部組件來完成,.net的出現,使這一工作變得非常容易并且可以隨便的使用bitmap和image類型。

在這個指導思想下,我將按照以下步驟(在你要上傳圖象文件上)創建一個簡單的web窗體,該窗體將判斷上傳的文件是否是jpeg文件、判斷該文件是否存在(必要時你可以重命名)。

1、  創建一個新web 應用程序項目;

2、  打開web 窗體;

3、  在窗體上面添加一個html表單,并把它轉換成服務器控件。在這個例子里,該文件將命名為filupload;(把html轉換成服務器控件的方法是,在它的上面右擊鼠標然后選擇run as server control)

4、  切換到html view并添加/更改form標簽的enctype屬性為multipart/form-data。如:enctype="multipart/form-data"。

5、  在web窗體上添加一個button并命名為btnupload。

6、  向web應用程序添加一個folder called /images。

7、  在窗體上添加一個web form image并命名為imgpicture,設置寬度和高度分別為160和120。

8、  添加一個label控件并命名為lbloutput。顯示當在上傳的過程中發生的任何錯誤。

9、  給按鈕btnupload的單擊事件添加如下代碼:(如果你想分析以下代碼的細節,你可以把下面的代碼復制粘貼到vs.net ide集成開發環境。)

1.     private void btnupload_click(object sender, system.eventargs e)
2.     {
3.         // initialize variables
4.         string ssavepath;
5.         string sthumbextension;
6.         int intthumbwidth;
7.         int intthumbheight;
8.     
9.         // set constant values
10.      ssavepath = "images/";
11.      sthumbextension = "_thumb";
12.      intthumbwidth = 160;
13.      intthumbheight = 120;
14.  
15.      // if file field isn’t empty
16.      if (filupload.postedfile != null)
17.      {
18.          // check file size (mustn’t be 0)
19.          httppostedfile myfile = filupload.postedfile;
20.          int nfilelen = myfile.contentlength;
21.          if (nfilelen == 0)
22.          {
23.              lbloutput.text = "no file was uploaded.";
24.              return;
25.          }
26.  
27.          // check file extension (must be jpg)
28.          if (system.io.path.getextension(myfile.filename).tolower() != ".jpg")
29.          {
30.              lbloutput.text = "the file must have an extension of jpg";
31.              return;
32.          }
33.  
34.          // read file into a data stream
35.          byte[] mydata = new byte[nfilelen];
36.          myfile.inputstream.read(mydata,0,nfilelen);
37.  
38.          // make sure a duplicate file doesn’t exist.  if it does, keep on appending an
39.          // incremental numeric until it is unique
40.          string sfilename = system.io.path.getfilename(myfile.filename);
41.          int file_append = 0;
42.          while (system.io.file.exists(server.mappath(ssavepath + sfilename)))
43.          {
44.              file_append++;
45.              sfilename = system.io.path.getfilenamewithoutextension(myfile.filename)
46.                               + file_append.tostring() + ".jpg";
47.          }
48.  
49.          // save the stream to disk
50.          system.io.filestream newfile
51.                  = new system.io.filestream(server.mappath(ssavepath + sfilename),
52.                                             system.io.filemode.create);
53.          newfile.write(mydata,0, mydata.length);
54.          newfile.close();
55.  
56.          // check whether the file is really a jpeg by opening it
57.          system.drawing.image.getthumbnailimageabort mycallback =
58.                         new system.drawing.image.getthumbnailimageabort(thumbnailcallback);
59.          bitmap mybitmap;
60.          try
61.          {
62.              mybitmap = new bitmap(server.mappath(ssavepath + sfilename));
63.  
64.              // if jpg file is a jpeg, create a thumbnail filename that is unique.
65.              file_append = 0;
66.              string sthumbfile = system.io.path.getfilenamewithoutextension(myfile.filename)
67.                                                       + sthumbextension + ".jpg";
68.              while (system.io.file.exists(server.mappath(ssavepath + sthumbfile)))
69.              {
70.                  file_append++;
71.                  sthumbfile = system.io.path.getfilenamewithoutextension(myfile.filename) +
72.                                 file_append.tostring() + sthumbextension + ".jpg";
73.              }
74.  
75.              // save thumbnail and output it onto the webpage
76.              system.drawing.image mythumbnail
77.                      = mybitmap.getthumbnailimage(intthumbwidth,
78.                                                   intthumbheight, mycallback, intptr.zero);
79.              mythumbnail.save (server.mappath(ssavepath + sthumbfile));
80.              imgpicture.imageurl = ssavepath + sthumbfile;
81.  
82.              // displaying success information
83.              lbloutput.text = "file uploaded successfully!";
84.  
85.              // destroy objects
86.              mythumbnail.dispose();
87.              mybitmap.dispose();
88.          }
89.          catch (argumentexception errargument)
90.          {
91.              // the file wasn't a valid jpg file
92.              lbloutput.text = "the file wasn't a valid jpg file.";
93.              system.io.file.delete(server.mappath(ssavepath + sfilename));
94.          }
95.      }
96.  }
97.  
98.  public bool thumbnailcallback()
99.  {
100.         return false;
}     

10.運行以上創建的 web頁(webpage),并分別使用jpg文件和其他類型的文件來測試錯誤檢查(error-checking)機制。

11. 如果你有什么問題或建議,請給作者留言。

-------------------------------------------------------------

關于chris khoo

i was born in malaysia on 13th february 1982 and moved to australia when i was 8. i also started programming around that time as well in gwbasic . over the years, i picked up assembly, pascal, and c/c++.

in my teen/adult years when windows programming became the norm, i also learnt some mfc, visual basic, and java and have completed some web projects utilizing asp and also created office vba macros for businesses.

within this time, i picked up a mcsd and mcse, however i finally chose to go to university instead of working a full time software development job.

currently, at age 20, i am studying commerce(accounting) and information technology at the university of queensland, and it is a blast.

i enjoy working with businesses to help grow them in whatever way (usually it involves it initially), challenging software/web development projects and learning new things everyday. (i'm into this .net stuff now).

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 千阳县| 清新县| 廉江市| 什邡市| 射阳县| 肇州县| 丰原市| 祁阳县| 定南县| 四川省| 清苑县| 苏尼特右旗| 荣成市| 南皮县| 介休市| 香港 | 囊谦县| 镇沅| 高邑县| 康保县| 余庆县| 永春县| 芷江| 莱阳市| 新平| 潜山县| 迁安市| 曲阳县| 屏东市| 阿克| 铜川市| 泾阳县| 益阳市| 上饶县| 宁化县| 镇沅| 额尔古纳市| 兴文县| 德江县| 米脂县| 石景山区|