圖片上傳和檔案夾建立、檔案上傳

來源:互聯網
上載者:User

標籤:

 

 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:\搜狗高速下載\Shop\Shop\Admin\Download\" + 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:\搜狗高速下載\Shop\Shop\Admin\Download\" + 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>

圖片上傳和檔案夾建立、檔案上傳

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.