首页 >> 租房资讯

通过上传图象牛

2021-09-06 来源:延安租房网

以前,通过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的单击事件添加如下代码:(如果你想分析以下代码的细节,你可以把下面的代码复制粘贴到 IDE集成开发环境。)

1. private void btnUpload_Click(object sender, entArgs 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 (stedFile != null)

17. {

18. // Check file size (mustn’t be 0)

19. HttpPostedFile myFile = stedFile;

20. int nFileLen = ntentLength;

21. if (nFileLen == 0)

22. {

23. xt = \"No file was uploaded.\";

24. return;

25. }

26.

27. // Check file extension (must be JPG)

28. if (tExtension(leName).ToLower() != \".jpg\")

29. {

30. xt = \"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. ad(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 = tFileName(leName);

41. int file_append = 0;

42. while (ists(pPath(sSavePath + sFilename)))

43. {

44. file_append++;

45. sFilename = tFileNameWithoutExtension(leName)

46. + file_String() + \".jpg\";

要记得放神恩) 47. }

48.

49. // Save the stream to disk

50. leStream newFile

51. = new leStream(pPath(sSavePath + sFilename),

52. eate);

53. ite(myData,0, ngth);

54. ose();

55.

56. // Check whether the file is really a JPEG by opening it

57. tThumbnailImageAbort myCallBack =

58. new tThumbnailImageAbort(ThumbnailCallback);

59. Bitmap myBitmap;

60. try

61. {

62. myBitmap = new Bitmap(pPath(sSavePath + sFilename));

63.

64. // If jpg file is a jpeg, create a thumbnail filename that is unique.

65. file_append = 0;

66. string sThumbFile = tFileNameWithoutExtension(leName)

67. + sThumbExtension + \".jpg\";

68. while (ists(pPath(sSavePath + sThumbFile)))

69. {

70. file_append++;

71. sThumbFile = tFileNameWithoutExtension(leName) +

72. file_String() + sThumbExtension + \".jpg\";

73. }

74.

75. // Save thumbnail and output it onto the webpage

76. age myThumbnail

77. = tThumbnailImage(intThumbWidth,

78. intThumbHeight, myCallBack, ro);

79. ve (pPath(sSavePath + sThumbFile));

80. ageUrl = sSavePath + sThumbFile;

81.

82. // Displaying success information

83. xt = \"File uploaded successfully!\";

84.

85. // Destroy objects

86. spose();

87. spose();

88. }

89. catch (ArgumentException errArgument)

90. {

91. // The file wasn\'t a valid jpg file

92. xt = \"The file wasn\'t a valid jpg file.\";

93. lete(pPath(sSavePath + sFilename));

94. }

95. }

96. }

97.

98. public bool ThumbnailCallback()

99. {

100. return false;

}

10.运行以上创建的 Web页(webpage),并分别使用JPG文件和其他类型的文件来测试错误检查(error-checking)机制。

查看本文来源

廊坊输卵管堵塞治疗费用多少钱
石家庄妇科专科
黄芪生脉饮和黄芪颗粒优势是什么