** 通过 .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. <