The database stores the picture path and displays the picture

Source: Internet
Author: User

These days in the image upload and display, upload the picture to save the folder and save the path to the database, display with data binding display

Make this picture control the path where the patch is attached

Before the online collection of relevant information, most people say that this path should be an absolute path rather than a relative path, so the storage path is open to save this is the case

But the running result is ....

The avatar here can't be displayed.

Later, with the help of seniors, the storage path was modified to

This is a relative path, and the VS read data starts looking in this "meta-space" folder (that is, starting from the root directory), Subordinate folder Images Search 20161124213555917.jpg is successful, if the path like the beginning is not found, so display as blank

The following are the results of the normal operation:

Success!

Here is the relevant code to upload the image

1 if(Upload1. HasFile)//To determine whether a control has a file path2                 {3                     stringfilename = upload1. FileName;//Get file name4filename = filename. Substring (filename. LastIndexOf (".") +1);//get suffix5                     if(filename. ToLower () = ="jpg"|| FileName. ToLower () = ="gif")//Judging Type6                     {7                         stringimg = DateTime.Now.ToString ("yyyymmddhhmmssfff") +"."+filename;8Upload1. SaveAs (Server.MapPath ("images/") +img);9                         stringPicture = ("images/") +img;Ten                         //upload to the root directory of the images folder + renamed file name, you can also use the original picture name, self-determined. Upload success; One                         stringsql ="INSERT into tabusers values ('"+ ID +"', '"+ name +"', '"+ Hspwd +"', '"+ Sex +"', '"+ year+"', '"+mail+"', '"+ Question1 +"', '"+ Hsanswer +"', '"+ Picture +"')"; A                         intRESULT2 =class.put (SQL); -                         if(RESULT2 = =1) - Binddata (); the                         Else if(Result2! =1) -                         { -Response.Write ("<script>alert (' Registration failed! ');</script>"); -                             return; +                         } -                     } +                     Else A                     { atResponse.Write ("<script>alert (' image format only supports JPG and GIF ');</script>"); -                         return;//Prompt Error -                     } -                 } -                 Else -                 { inResponse.Write ("<script>alert (' Please choose photos! ');</script>"); -                     return;//Prompt Error to}

Look inside. "

String picture = ("images/") + img;

", Save the Path

The database stores the picture path and displays the picture

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.