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>

  • 相关阅读:
    double保存小数点后两位
    二维数组做函数参数,及地址问题
    解决应用程序无法正常启动0xcxxxxxxxxxx问题
    docker+selenium grid解决node执行经常卡死
    docker+selenium Grid搭建自动化分布式测试环境
    Docker+Selenium Grid+Python搭建分布式测试环境
    xshell无法连接Ubuntu的解决办法
    docker获取镜像很慢解决办法
    django使用ajax传输数据
    $.ajax()所有参数详解
  • 原文地址:https://www.cnblogs.com/G-XiaoHua/p/4832267.html
Copyright © 2011-2022 走看看