vb WebSite 上传图片,图片保存到制定文件夹,数据库只保存图片路径和名称

前端之家收集整理的这篇文章主要介绍了vb WebSite 上传图片,图片保存到制定文件夹,数据库只保存图片路径和名称前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
vb WebSite 上传图片图片保存到制定文件夹,数据库只保存图片路径和名称

因为我的图片是用 javascript 显示出来的,所以我的页面不能用模版,页面代码如下

 <script>
function setImagePreview() {
            var docObj = document.getElementById("doc");

            var imgObjPreview = document.getElementById("preview");
            if (docObj.files && docObj.files[0]) {
                //火狐下,直接设img属性
                imgObjPreview.style.display = 'block';
                imgObjPreview.style.width = '300px';
                imgObjPreview.style.height = '120px';
                //imgObjPreview.src = docObj.files[0].getAsDataURL();

                //火狐7以上版本不能用上面的getAsDataURL()方式获取,需要一下方式  
                imgObjPreview.src = window.URL.createObjectURL(docObj.files[0]);

            } else {
                //IE下,使用滤镜
                docObj.select();
                var imgSrc = document.selection.createRange().text;
                var localImagId = document.getElementById("localImag");
                //必须设置初始大小
                localImagId.style.width = "300px";
                localImagId.style.height = "120px";
                //图片异常的捕捉,防止用户修改后缀来伪造图片
                try {
                    localImagId.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(sizingMethod=scale)";
                    localImagId.filters.item("DXImageTransform.Microsoft.AlphaImageLoader").src = imgSrc;
                } catch (e) {
                    alert("您上传图片格式不正确,请重新选择!");
                    return false;
                }
                imgObjPreview.style.display = 'none';
                document.selection.empty();
            }
            return true;
        }

    </script>
 
                    <table border="0">
                        <tr>
                            <td colspan="2">
                          <div id="localImag" >
                          <img alt="" id="preview" runat="server"  src=""/>
                          </div>
                            </td>
                        </tr>
                         <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>
                            <td>  
                                <asp:Button ID="bt_image_save" Text="保存图片" runat="server" Width="60" />

                            </td>
                        </tr>
                    </table>
后台代码为:
 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 = "图片太大"
            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 = "图片格式不对"
                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 If
        End If       
    End Sub
原文链接:https://www.f2er.com/vb/259667.html

猜你在找的VB相关文章