Image Upload page written by Asp.net

Source: Internet
Author: User

ASPX page code

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "default. aspx. cs" inherits = "_ default" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div style = "text-align: Center">
<Span style = "font-size: 32pt; color: #99cc00">
Upload images </span> <br/>
<Br/>
<Asp: Image id = "image1" runat = "server" Height = "218px" width = "286px"/> <br/>
<Br/>
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = ""/> <br/>
<Br/>
<Asp: Label id = "label1" runat = "server" text = "label" forecolor = "red"> </ASP: Label> </div>
</Form>
</Body>
</Html>
CS Page code

Using system;
Using system. Data;
Using system. configuration;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Public partial class _ default: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{
Image1.visible = false;
Label1.visible = false;
}
Protected void button#click (Object sender, eventargs E)
{
String picname = fileupload1.filename; // obtain the name of the uploaded file.
Int picsize = fileupload1.postedfile. contentlength; // obtain the size of the uploaded file
String pictype = picname. substring (picname. lastindexof ("."); // obtain the Upload File Type
String posttime = datetime. Now. tostring ("yyyymmddhhmmss"); // obtain the upload time string yyyymmddhhmmss-> every year, month, day, hour, minute, second
String picpathsuccess = server. mappath ("picuploadsuccess") + "//" + posttime + pictype; // obtain the physical path of the uploaded file. Note the following: "//"
String picpathfailed = server. mappath ("picuploadfailed") + "//" + posttime + pictype; // obtain the physical path of the file that fails to be uploaded. Note the following: "//"
If (pictype = ". jpg "| pictype = ". GIF "| pictype = ". jpg "| pictype = ". GIF "| pictype = ". JPEG "| pictype = ". JPEG ")
{
Fileupload1.saveas (picpathsuccess); // Save the uploaded file to the server
Label1.text = "the image is uploaded successfully. The image name is:" + picname + "the image size is:" + picsize + "Byte ";
Label1.visible = true;
Image1.imageurl = "picuploadsuccess //" + posttime + pictype; // relative path and absolute path.
Image1.visible = true;
}
Else
{
Fileupload1.saveas (picpathfailed );
Label1.text = "Image Upload Failed. Image name:" + picname + "image size:" + picsize + "Byte ";
Label1.visible = true;
Image1.imageurl = "picuploadfailed // picuploadfailed. jpg ";
Image1.visible = true;
}
}
}

Supplement: absolute path vs relative path

Absolute path:

When we usually use a computer, we need to know the location of the file to find the desired file, and the path to indicate the location of the file is the path, for example, as long as we see this path: C: /website/img/photo.jpgwe know that the photo.jpg file is in the IMG subdirectory under the website directory of drive C. The path similar to this complete description file location is an absolute path. Http: // www. e3i5. Net/Img/photo.jpg is also an absolute path to determine the file location.
  

Relative Path:

Analyze why the image cannot be properly displayed. Now there is a page index.htm, which is connected with an image photo.jpg. Their absolute path is as follows:
C:/website/index.htm
C:/website/img/photo.jpg

If you use the absolute path C:/website/img/photo.jpg, everything works on your computer, because it can indeed be C at the specified position: /website/img/photo.jpgto find the photo.jpg file, but when you upload the page to the website, it is very likely that an error will occur, because your website may be on the drive C of the server, or disk D, it may also be in the AA directory, or in the BB directory. In short, there is no reason for a path such as C:/website/img/photo.jpg. In the index.htm file, how can I use the example to locate the photo.jpg file? Yes, it should be a relative path. The so-called relative path, as its name suggests, is its own relative location to the target. In the previous example, you can use img/photo.jpg to locate the file attached to index.htm. no matter where these files are stored, as long as their relative relationship remains unchanged, no errors will occur.

In addition, we use "../" to represent the upper-level directory, "../" to represent the upper-level directory, and so on.

In this example, all examples are attached with an image photo.jpg in the index.htmfile.

Example:
C:/website/web/index.htm
C:/website/img/photo.jpg
In this example, how should photo.jpg connected to index.htmbe be represented?
Incorrect syntax: img/photo.jpg
This method is incorrect. The correct method is to use the relative path of ../img/photo.jpg to locate the file.

  

Conclusion: The above example shows that when converting an absolute path to a relative path, the same parts of the two absolute paths can be ignored. You only need to consider their differences.

Related Article

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.