VB website uploads images and saves the images to the specified folder. The database only saves the image path and name.

Source: Internet
Author: User
VB website uploads images and saves them to the specified folder. The database only saves the image path and name;

Because my images are displayed in Javascript, templates cannot be used for my pages. The page code is as follows:

<Script> function setImagePreview () {var docObj = document. getElementById ("doc"); var imgObjPreview = document. getElementById ("preview"); if (docObj. files & docObj. files [0]) {// under Firefox, set the img attribute imgObjPreview directly. style. display = 'block'; imgObjPreview. style. width = '300px '; imgObjPreview. style. height = '120px '; // imgObjPreview. src = docObj. files [0]. getAsDataURL (); // Firefox 7 or later versions cannot be obtained using the getAsDataURL () method. ImgObjPreview. src = window. URL. createObjectURL (docObj. files [0]);} use the filter docObj under else {// IE. select (); var imgSrc = document. selection. createRange (). text; var localImagId = document. getElementById ("localImag"); // The initial size must be set to localImagId. style. width = "300px"; localImagId. style. height = "120px"; // capture Abnormal images to prevent users from modifying the suffix to counterfeit images. try {localImagId. style. filter = "progid: DXImageTransform. microsoft. alphaImageLoader (SizingMethod = scale) "; localImagId. filters. item ("DXImageTransform. microsoft. alphaImageLoader "). src = imgSrc;} catch (e) {alert ("the format of the image you uploaded is incorrect. Please reselect it! "); Return false;} imgObjPreview. style. display = 'none'; document. selection. empty ();} return true ;}</script>
<Table border = "0"> <tr> <td colspan = "2"> <div id = "localImag">  </div> </td> </tr> <td colspan = "2"> <asp: label ID = "lblMessage" runat = "server" Text = "" ForeColor = "Red" Font-Bold = "true"/> </td> </tr> <tr> <td> <input type = "file" name = "doc" id = "doc" runat = "server" onchange = "javascript: setImagePreview (); "/> </td> <asp: button ID = "bt_image_save" Text = "Save image" runat = "server" Width = "60"/> </td> </tr> </table>
The background code is:
Private Sub bt_image_save_Click (ByVal sender As System. object, ByVal e As System. eventArgs) Handles bt_image_save.Click Dim img As String Dim postedfile As HttpPostedFile = doc. postedFile Dim intImgSize As Int32 intImgSize = postedfile. contentLength If intImgSize <> 0 Then If intImgSize> 800 Then lblMessage. text = "the image is too large" Else Dim strImgType As String = postedfile. contentType Dim filesplit () As String = Split (strImgType, "/") strImgType = filesplit (filesplit. length-1) if strImgType <> "gif" And strImgType <> "jpg" And strImgType <> "pjpeg" And strImgType <> "jpeg" And strImgType <> "bmp" And strImgType <>" png "Then lblMessage. text = "incorrect image format" Else lblMessage. text = "" filesplit = Split (postedfile. fileName, "\") Dim filename As String = filesplit (filesplit. length-1) postedfile. saveAs (Server. mapPath ("headimg") & "\" & filename) Dim imgpath As String = "headimg \" & filename img = imgpath Dim conn As SqlClient. sqlConnection = DBHelp. getNewConnection conn. open () Dim cmd As New SqlClient. sqlCommand ("update pmc_emp set emp_image = @ img where emp_no = @ emp_no", conn) cmd. parameters. add ("@ img", SqlDbType. varChar ). value = img cmd. parameters. add ("@ emp_no", SqlDbType. NVarChar ). value = txtEmpNo. text. trim Try cmd. executeNonQuery () SetImage () Catch ex As Exception PageHelp. showErrorMsg (Page) Finally conn. close () End Try End If End Sub
 
 

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.