zoukankan      html  css  js  c++  java
  • 上传图片

    上传图片
    aspx前端:

    <div>
        <asp:Image ID="Image1" runat="server" />//这个控件在浏览器中不显示 <span style="color:#eeff00">图片预览</span>
        <asp:TextBox ID="txtCover" runat="server" ReadOnly=“true”></asp:TextBox>//显示存储图片的路径 
        <span style="color:#4cff00">图片上传的路径</span> </br>
        <asp:FileUpload ID="imgupload" runat="server" />&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="btnUpload" runat="server" Text="上传" OnClick="btnUpload_Click" /> 
       //OnClick是鼠标点击跳转的事件 btnUpload_Click 是执行protected void btnUpload_Click(object sender, EventArgs e)这个事件 
    </div>

    aspx.cs后台

    protected void btnUpload_click(object sender, EventArgs e)
    {
        if(imgupload.PostedFile.FileName != "")
        {
            string imgPath = imgupload.PostedFile.FileName;
            string extPath = imgPath.Substring(imgPath.LastIndexOf(".") + 1)
            if(extPath.ToLower() != "jpg" && extPath.ToLower() != "png" && extPath.ToLower() != "bmp")
            {
                Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('选择文件类型JPG,bmp,PNG')</script>");
            }
            else
            {
                string sPath = Server.MapPath("/UploadFiles/upload");
                imgupload.PostedFile.SaveAs(sPath + "\" + imgPath.Substring(imgPath.LastIndexOf("\") + 1); 
    sPath = sPath.Substring(sPath.LastIndexOf("\") + 1); //sPath = 文件夹里的upload; //在这里Spath被重置为\upload this.txtCover.Text = sPath + "/" + imgPath.Substring(imgPath.LastIndexOf("\") + 1); //文件夹里的upload+xx.jpg; // 这里txtCover的值为upload文件名.扩展名 Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('上传成功')</script>");
    Image1.ImageUrl = "/UploadFiles/" + this.txtCover.Text; //?upload/$@B0)U`]TDA@MKRDW15BJQR.jpg } else { Page.ClientScript.RegisterClientScriptBlock(GetType(), "", "<script language=javascript>alert('请选择图片')</script>"); } } }
  • 相关阅读:
    基于bootstrap实现收缩导航条
    js判断打开网站页面是PC端还是手机端
    vs2015调试sqlserver 存储过程
    C#计算当前日是第几周
    C#计算一年有多少周
    tcpdump命令的使用
    keepalived配置
    正则匹配中文
    git merge 和 git rebase详解
    systemd和sysv服务管理和配置
  • 原文地址:https://www.cnblogs.com/ZkbFighting/p/8290052.html
Copyright © 2011-2022 走看看