zoukankan      html  css  js  c++  java
  • 图片上传和文件夹创建、文件上传

     protected void btnUp_Click(object sender, EventArgs e)
            {


                string savePath = @"images/";//图片保存路径
                string fileName = Server.HtmlEncode(picupload.FileName);
                string extension = System.IO.Path.GetExtension(fileName).ToLower();//取得扩展名
                string ImageName = "";
                String[] allowExtension = { ".jpg", ".jif", ".jpeg", ".jpe", ".png", ".bmp" };



                for (int i = 0; i < allowExtension.Length; i++)
                {



                    if (extension == allowExtension[i] && fileName != "nopic.jpg")
                    {

                        ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + extension;
                        savePath += ImageName;
                        savePath = Server.MapPath(savePath);
                        picupload.SaveAs(savePath);

                        Response.Write("<script>alert('图片上传成功'),location.href='Test.aspx'</script>");
                    }

                    else
                    {
                        Label1.Text = "请选择jpg, jif, jpeg, jpe, png, bmp格式的图片";


                    }



                }


                #region 文件上传
                String Year = DateTime.Now.Year.ToString();
                String Month = DateTime.Now.Month.ToString();
                String Day = DateTime.Now.Day.ToString();

                String Time = Year + Month + Day;

                //创建文件夹的绝对路径
                String Path = @"D:搜狗高速下载ShopShopAdminDownload" + Time;
                //上传文件的路径
                String savePath = @"Download\" + Time + "\";
                //判断是否存在这个文件夹
                if (!Directory.Exists(Path))
                {
                    //创建文件夹
                    Directory.CreateDirectory(Path);


                    savePath = File(savePath);


                }
                else
                {

                    savePath = File(savePath);


                }

                #endregion

     private String File(String savePath)
            {
                //文件保存路径

                string fileName = Server.HtmlEncode(FileUpload1.FileName);
                string extension = System.IO.Path.GetExtension(fileName).ToLower();//取得扩展名
                string ImageName = "";
                String[] allowExtension = { ".txt", ".docx", ".xls", ".pptx", ".xlsx", ".doc" };



                for (int i = 0; i < allowExtension.Length; i++)
                {



                    if (extension == allowExtension[i])
                    {

                        ImageName = fileName;
                        savePath += ImageName;
                        savePath = Server.MapPath(savePath);
                        FileUpload1.SaveAs(savePath);
                        HiddenField1.Value = savePath;
                        Label1.Text = "";




                    }



                    else
                    {
                        if (HiddenField1.Value == "")
                        {
                            Label1.Text = "请选择txt, docx, xls, pptx, xlsx, doc格式的文件";
                        }


                    }
                }

                return savePath;
            }

    //创建文件夹的绝对路径
                String Path = @"D:搜狗高速下载ShopShopAdminDownload" + Time;
                //上传文件的路径
                String savePath = @"Download\" + Time + "\";
                //判断是否存在这个文件夹
                if (!Directory.Exists(Path))
                {
                    Directory.CreateDirectory(Path);

                    //创建文件夹
                    savePath = File(savePath);
                }
                else
                {

                    savePath = File(savePath);
                }

    <div>
            <asp:ScriptManager ID="ScriptManager1" runat="server">
            </asp:ScriptManager>
            <asp:Button ID="btnUp" runat="server" Text="上传图片" OnClick="btnUp_Click" />
            <asp:FileUpload ID="picupload" runat="server" />
            <asp:UpdatePanel ID="UpdatePanel1" runat="server">
                <ContentTemplate>
                    <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label>
                </ContentTemplate>
            </asp:UpdatePanel>
            <img src="images/20140915024843589.jpg" width="100px" />
        </div>

  • 相关阅读:
    区块链系列教程
    第三章 通过java SDK 实现个性化智能合约的部署与测试
    第一章 区块链系列 联盟链FISCO BCOS 底层搭建
    ABP 框架 数据库底层迁移 Mysql 集群
    ABP 框架代码批量生成器
    基于百度理解与交互技术实现机器问答
    微软人工智能和对话平台--知识商城体验
    基于百度AI实现 车牌识别
    最近整理AI相关感想
    百度OCR文字识别-身份证识别
  • 原文地址:https://www.cnblogs.com/G-XiaoHua/p/4832267.html
Copyright © 2011-2022 走看看