Use the File Field control on the client to upload images.

Source: Internet
Author: User
Add a File Field and Image control for the client on the page, right-click the File Field control, make it run as a control on the server side to upload images to a folder on the server (the folder where images are saved is called UpImages), and save the image name in the database, the image name is changed to the current time when it is uploaded, so that when there are too many images, the original image will not be overwritten, and the size of the image will be controlled. When you select the correct image, the IMAGE is displayed in the IMAGE control.
There is a problem in this instance, that is, why document is displayed after a dialog box pops up when the file you selected is not the correct image suffix. getElementById ("myFile "). value = "" This sentence cannot empty the content in File Field, so I made another judgment on the server side. If someone else has a high opinion, I hope to post a comment. Thank you.

When the image is displayed later, the image name is taken out, and the image can be displayed on the page according to the image path. The same is true for displaying the image in the DataGrid.
For example, you can click an image in the template column to go to the corresponding page.
<A href = '<% # DataBinder. eval (Container, "DataItem. homepage ") %> 'target = _ blank> 'width = 100 border = 0> </A>
HTML code:
<Body MS_POSITIONING = "GridLayout">
<Script>
Function checkData ()
{
Var fileName = document. getElementById ("myFile"). value;
If (fileName = "")
Return;
Var exName = fileName. substr (fileName. lastIndexOf (".") + 1). toUpperCase ()
// Alert (exName)
If (exName = "JPG" | exName = "BMP" | exName = "GIF ")
{
Document. getElementById ("myimg"). src = fileName
}
Else
{
Alert ("select the correct image file ")
Document. getElementById ("myFile"). value = ""
}
}
</Script>
<Form id = "Form1" method = "post" runat = "server">
<Table align = "center" border = "1" width = "80%">
<Tr>
<Td align = "center" height = "30"> <font style = "FONT-SIZE: 10pt"> image: </font> </td>
<Td height = "30"> & nbsp; <INPUT id = "myFile" type = "file" onchange = "checkData () "size =" 34 "runat =" server "NAME =" myFile ">
& Nbsp; <font style = "FONT-SIZE: 10pt "> (image files cannot exceed 200 KB) </font> </td>
</Tr>
<Tr>
<Td colspan = "2" align = "center">
<Asp: Button id = "btnSubmit" runat = "server" Text = "OK" Width = "77px" CssClass = "redButtonCss"> </asp: Button> </td>
</Tr>
</Table>
</Form>
</Body>

Server-side submission event

Private void btnSubmit_Click (object sender, System. EventArgs e)
{
String strImageName = "";
String FileName = myFile. Value;
String Publishtime = DateTime. Now. ToString ();
If (FileName = "") // when no image exists, the image name saved to the database is empty.
{
Bool result = DUI. Insert_UpImage (strImageName, Publishtime); // This is the method used to save the image to the database.
If (result)
{
Response. Write ("<script> alert ('image saved successfully') </script> ");
Response. Write ("<script> location. href = location. href </script> ");
}
}
Else
{
String exName = FileName. Substring (FileName. LastIndexOf (".") + 1). ToUpper (); // The suffix of the captured image
If (exName = "JPG" | exName = "BMP" | exName = "GIF ")
{
If (myFile. PostedFile. ContentLength> 204800) // checks whether the image is larger than 200 kb.
{
Response. Write ("<script> alert ('Sorry, the image you uploaded is too large. Please convert it and upload it later ') </script> ");
Return;
}

// Generate image names by Time
String SaveName = DateTime. Now. ToString ("yyyyMMddhhmmssfff ");

String fn = myFile. PostedFile. FileName;
StrImageName = SaveName + fn. Substring (fn. LastIndexOf ("."); // image name and suffix
String strpath = Server. MapPath ("") + "\ UpImages \" + strImageName; // obtain the path to save the image
MyFile. PostedFile. SaveAs (strpath); // Save the image in this path

Bool result = DUI. Insert_UpImage (strImageName, Publishtime); // This is the method used to save the image to the database.
If (result)
{
Response. Write ("<script> alert ('image input succeeded ') </script> ");
Response. Write ("<script> location. href = location. href </script> ");
}
}
Else
{
Response. Write ("<script> alert ('select the correct image file') </script> ");
Return;
}
}
}

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.