Upload image by .NET

zhaozj2021-02-16  61

Upload image by .NET

Author: Chris Khoo translation: tigerwen01

Previously, through the ASP upload image (the size of the image, the type is limited), it is generally done with external components, and the .NET appears, make this work very easy and can use Bitmap and Image types. .

Under this guiding ideology, I will follow the steps below (on the upload image file) to create a simple web form, which will determine if the uploaded file is a JPEG file, determine if the file exists (if necessary You can rename).

1. Create a new web application project;

2, open the web form;

3. Add an HTML form to the form and convert it into a server control. In this example, the file will be named FILUPLOAD; (Convert HTML to the server control method, right-click the mouse on it and then select Run As Server CONTOL)

4. Switch to HTML View and add / change the ENCTYPE attribute of the Form tag to Multipart / Form-Data. Such as: encType = "Multipart / Form-Data".

5. Add a button on the web form and name BtNupload.

6. Add a Folder Called / Images to the web application.

7. Add a web form image on the form and name IMGPicture, setting the width and heights of 160 and 120, respectively.

8, add a Label control and named lbloutput. Display Any error that occurs during the upload process.

9. Add the following code to the click event of the button btNupload: (if you want to analyze the details of the following code, you can copy the following code to the VS.NET IDE integration development environment.)

1. Private void btnupload_click (Object Sender, System.EventArgs E)

2. {

3. // Initialize Variables

4. String ssavepath;

5. String stumbextension;

6. INT INTTHUMBWIDTH;

7. 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)

twenty two. {

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 harve 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 WHETER 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 stumbfile = 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. IntethumbHeight, 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. Run the web page (WebPage) created, and use the JPG file and other types of files to test the error check (ERROR-Checking) mechanism.

11. If you have any questions or suggestions, please give the author message.

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

About Chris Khoo

I Was Born in Malaysia On 13th February 1982 and Moved to Australia When I Was 8. I Also Started Programming Around Time As Well in GWBasic. Over the year, 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 UPA 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).

转载请注明原文地址:https://www.9cbs.com/read-23237.html

New Post(0)