通过.NET上传图象
作者:Chris Khoo 翻译:tigerwen01
以前,通过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.
本文地址:http://com.8s8s.com/it/it45291.htm