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>

  • 相关阅读:
    Firemonkey 控件设定字型属性及颜色
    ListView 使用 LiveBindings 显示超过 200 条记录
    Firemonkey ListView 获取项目右方「>」(Accessory) 事件
    XE7 Update 1 选 iOS 8.1 SDK 发布 iPhone 3GS 实机测试
    Firemonkey Bitmap 设定像素颜色 Pixel
    Firemonkey 移动平台 Form 显示使用 ShowModal 范例
    XE7 提交 App(iOS 8)提示「does not contain the correct beta entitlement」问题修复
    XE7 Android 中使用 MessageDlg 范例
    导出 XE6 预设 Android Style (*.style) 档案
    修正 Memo 設定為 ReadOnly 後, 無法有複製的功能
  • 原文地址:https://www.cnblogs.com/G-XiaoHua/p/4832267.html
Copyright © 2011-2022 走看看