Upload, display, and store the image information in the Database Table 9.17

Source: Internet
Author: User
For the image uploading in the food module, I mainly added the upload Image Display Section. Here, I separated the image uploading from the information storage. I mainly used the label control and added two labels, A button is used for uploading, an image (ID: dismc) control is used to display the image, a label (ID: mappath) control is used to display the physical path of the uploaded image, and a label (ID: mcphoto) controls are used to record the virtual paths of uploaded images. This value is passed to qiaomenphoto in the database table. The file address of the image control. By the way, the judgment of the file format is also added, so that this part of the uploaded image is complete. Unfortunately, I will not package it now. The specific code is as follows: protected void upload_click (Object sender, eventargs e) {// the upload here uses the classic upload Boolean fileok = false on msdn; // used to determine the format of the uploaded file // returns the physical file path string Path = server corresponding to the specified virtual path on the Web server. mappath ("~ /Meishi/"); If (photoupload. hasfile) // determine whether the control contains files {// obtain the extension of the uploaded file and convert it to lower case string fileextensions = system. io. path. getextension (photoupload. filename ). tolower (); // defines the array string [] allowedextension = {". JPEG ",". jpg ",". BMP ",". GIF ",". tiff ",". EXIF ",". PNG ",". icon "}; For (INT I = 0; I <allowedextension. length; I ++) // image format judgment {If (fileextensions = allowedextension [I]) {fileok = true ;}} if (F Ileok) // upload the image {// rename the image, which can be abbreviated. However, currently, only the string sfilename = photoupload can be achieved. postedfile. filename; sfilename = sfilename. substring (sfilename. indexof (". "); // get the file extension random rannumber = new random (); // instantiate a random number datetime timenow = datetime. now; string time = timenow. tostring (); time = time. replace (":", ""); // remove the time format:,-, and space time = time. replace ("-", ""); time = time. replace ("", ""); // rename the image = Current Time + four bits Number of hosts + image extension sfilename = Time + rannumber. next (1000,999 9) + sfilename; string wpath = "meishi/" + sfilename; // put the image in this folder try {// Save the content of the uploaded file, stored in the photoupload directory. postedfile. saveas (path + sfilename); success. TEXT = "image uploaded! "; Mappath. TEXT = photoupload. postedfile. filename; // obtain the complete path of the uploaded file, which is dismc. imageurl = wpath; // displays the uploaded image mcphoto. TEXT = wpath; // This information is used to insert to the database} catch (system. exception exe) {success. TEXT = "An error occurred while uploading the image! ";}} Else {success. Text =" this type of image cannot be uploaded! ";}}

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.