zoukankan      html  css  js  c++  java
  • FileUpLoad控件

    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <asp:Image ID="Image" runat="server" width="50" Height="50"/>
            <br />
            <asp:TextBox ID="info" runat="server"></asp:TextBox>
            <br />
     
            <asp:FileUpload ID="up" runat="server" />
            <br/>
            <asp:Button ID="btnupLoad" runat="server" Text="Button" 
                onclick="btnupLoad_Click" />
        </div>
        </form>
    </body>
    </html>

    后套代码:

    protected void btnupLoad_Click(object sender, EventArgs e)
        {
            if (up.HasFile)
            {
                if (up.PostedFile.ContentType.Substring(0, 5) == "image")
                {
                    try
                    {
                        string serverPath = Server.MapPath("upLoad");
                        if (!System.IO.Directory.Exists(serverPath))
                        {
                            System.IO.Directory.CreateDirectory(serverPath);
                        }
                        string imgName = up.FileName;
                        string newPath = serverPath + "\" + imgName;
                        up.SaveAs(newPath);
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传成功');", true);
                        info.Text = "";
                        Image.ImageUrl = "upLoad/" + imgName;
                        info.Text += "路径:" + up.PostedFile.ContentType;
                    }
                    catch
                    {
                        ClientScript.RegisterStartupScript(this.GetType(), "", "alert('上传失败');", true);
                    }
                }
                else
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择图片');", true);
                }
            }
            else
            {
                ClientScript.RegisterStartupScript(this.GetType(), "", "alert('选择要上传的图片');", true);
            }
        }

     ClientScript.RegisterStartupScript(this.GetType(), "", "alert('请选择图片');", true);

    若是gif图像,则ContentType的值为image/gif

  • 相关阅读:
    [JZOJ3386] 守卫者的挑战
    [JZOJ3385] 黑魔法师之门
    [JZOJ3383] 太鼓达人
    [JZOJ3382] 七夕祭
    NOIP模拟测试on 2019.9.27
    数据结构测试2 on 2019.9.25
    数据结构测试1 on 2019.9.24
    P2047 [NOI2007]社交网络
    P2286 [HNOI2004]宠物收养场
    P1342 请柬 建反图+dijkstra
  • 原文地址:https://www.cnblogs.com/qiushuixizhao/p/3312017.html
Copyright © 2011-2022 走看看