首页 > 编程 > .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).

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表