Pass. NET Upload image

Source: Internet
Author: User
Tags file size html form tostring valid
Upload

Previously, upload images via ASP (image size, type are limited) is generally to use external components to complete. NET makes this work very easy and can be casually used bitmap and image types.

Under this guideline, I will follow the steps below (in the image file you want to upload) to create a simple Web form that will determine whether the uploaded file is a JPEG file, and determine whether it exists (you can rename it if necessary).

1, create a new Web application project;

2, open the Web form;

3, add an HTML form above the form and convert it to a server control. In this case, the file will be named Filupload; (the way to convert HTML to a server control is to right-click on it and choose Run As Server)

4, switch to HTML view and Add/Change form label Enctype property is Multipart/form-data. such as: Enctype= "Multipart/form-data".

5, add a button on the Web form and name it btnupload.

6, add a folder Called/images to the Web application.

7. Add a Web Form image to the form and name it imgpicture, setting the width and height to 160 and 120 respectively.

8, add a Label control and name it lbloutput. Displays any errors that occur during the upload process.

9. Add the following code to the Click event for the button btnupload: (If you want to analyze the details of the following code, you can copy and paste the following code into the Vs.net IDE integrated development environment.) )

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
Ssavepath = "images/";
Sthumbextension = "_thumb";
Intthumbwidth = 160;
Intthumbheight = 120;
14.
//If file field isn ' t empty
if (filupload.postedfile!= null)
17. {
//Check File size (mustn ' t be 0)
Httppostedfile myFile = filupload.postedfile;
int nfilelen = Myfile.contentlength;
if (Nfilelen = 0)
22. {
Lbloutput.text = "No file was uploaded.";
return;
25.}
26.
//Check file extension (must be JPG)
if (System.IO.Path.GetExtension (myfile.filename). ToLower ()!= ". jpg")
29. {
Lbloutput.text = "The file must have an extension of JPG";
return;
32.}
33.
//Read file into a data stream
byte[] MyData = new Byte[nfilelen];
MyFile.InputStream.Read (Mydata,0,nfilelen);
37.
//Make sure a duplicate file doesn ' t exist. If it does, keep on appending
Incremental//numeric until it is unique
String sfilename = System.IO.Path.GetFileName (myfile.filename);
File_append int = 0;
while (System.IO.File.Exists (Server.MapPath (Ssavepath + sfilename)))
43. {
file_append++;
sFileName = System.IO.Path.GetFileNameWithoutExtension (myfile.filename)
+ File_append. ToString () + ". jpg";
47.}
48.
//Save the stream to disk
System.IO.FileStream NewFile
Wuyi = new System.IO.FileStream (Server.MapPath (Ssavepath + sfilename),
System.IO.FileMode.Create);
Newfile.write (mydata,0, mydata.length);
Newfile.close ();
55.
//Check Whether the file is really a JPEG by opening it
System.Drawing.Image.GetThumbnailImageAbort Mycallback =
New System.Drawing.Image.GetThumbnailImageAbort (Thumbnailcallback);
Bitmap Mybitmap;
Try
61. {
Mybitmap = new Bitmap (Server.MapPath (Ssavepath + sfilename));
63.
The.//If jpg file is a jpeg, create a thumbnail filename this is unique.
file_append = 0;
A. String sthumbfile = System.IO.Path.GetFileNameWithoutExtension (myfile.filename)
+ sthumbextension + ". jpg";
while (System.IO.File.Exists (Server.MapPath (Ssavepath + sthumbfile)))
69. {
file_append++;
Sthumbfile = System.IO.Path.GetFileNameWithoutExtension (myfile.filename) +
File_append. ToString () + sthumbextension + ". jpg";
73.}
74.
Thumbnail//Save and output it onto the webpage
System.Drawing.Image Mythumbnail
Mybitmap.getthumbnailimage = Intthumbwidth,
Intthumbheight, Mycallback, IntPtr.Zero);
Mythumbnail.save (Server.MapPath (Ssavepath + sthumbfile));
Imgpicture.imageurl = Ssavepath + sthumbfile;
81.
Success//displaying information
Lbloutput.text = "File uploaded successfully!";
84.
//Destroy objects
Mythumbnail.dispose ();
Mybitmap.dispose ();
88.}
ArgumentException catch (errargument)
90. {
wasn//The file ' t a valid JPG file
Lbloutput.text = "The file wasn ' t a valid JPG file."
System.IO.File.Delete (Server.MapPath (Ssavepath + sfilename));
94.}
95.}
96.}
97.
public bool Thumbnailcallback ()
99. {
return false;
}

10. Run the Web page created above (webpage) and test the error checking (error-checking) mechanism using JPG files and other types of files, respectively.

11. If you have any questions or suggestions, please leave a message to the author.

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

About Chris Khoo

I was born in Malaysia on the 13th February 1982 and moved to Australia where I was 8. I also started programming around that time as very in Gwbasic. Over the years, I picked up Assembly, Pascal, and C + +.

In my Teen/adult years when Windows programming became the norm, I also learnt some MFC, Visual Basic, and Java and have C ompleted 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 I am studying Commerce (Accounting) and information Technology at the University of Queensland, and I T 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).



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.