asp.net文件上傳和下載

atts 9年前發布 | 1K 次閱讀 C# ASP.NET

.cs代碼



/// <summary>

        /// 文件上傳
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button1_Click(object sender, EventArgs e)
        {
            string WebPath = HttpContext.Current.Server.MapPath("/UpLoad");//上傳到指定的文件夾
            string filename = FileImg.PostedFile.FileName;//文件名
            //上傳文件是否為空
            if (FileImg.PostedFile != null && FileImg.PostedFile.ContentLength > 0)
            {
                string imageType = Path.GetExtension(FileImg.PostedFile.FileName).ToLower();//獲取文件格式
                string s = ".bmp;.rar;.zip;.iso;.jpg;.jpeg;.gif;.png;.flv;.doc;.docx;.xls;.xlsx;.ppt;.pptx;.txt;.pps;.pdf";
                if (s.Split(';').Contains(imageType))
                {
                    //檢查是否有此文件夾
                    if (!Directory.Exists(WebPath))
                    {
                        System.IO.Directory.CreateDirectory(WebPath);
                    }
                    //上傳
                    FileImg.PostedFile.SaveAs(WebPath + "\\" + filename);
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('成功上傳文件!!')</script>", false);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('格式不正確')</script>", false);
                }
            }
            else
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "a", "<script>alert('請上傳文件!!')</script>", false);
            }
        }


        /// <summary>
        /// 文件下載
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Button2_Click(object sender, EventArgs e)
        {
            string filename = "aaa.txt";
            string filepath = Server.MapPath("DownLoad/aaa.txt");


            //以字符流的形式下載文件


            FileStream fs = new FileStream(filepath, FileMode.Open);
            byte[] bytes=new byte[(int)fs].Length];
            fs.Read(bytes,0,bytes.Length);
            fs.Close();


            Response.ContentType="application/octet-stream";
            //通知瀏覽器下載而不是打開


             Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
             Response.BinaryWrite(bytes);
             Response.Flush();
             Response.End();

        }



前臺

  <form id="form1" runat="server">
    <div>
       <input type="file" onchange="previewImage(this)" id="FileImg" runat="server" />
       <asp:Button ID="Button1" runat="server" Text="保存圖片" OnClick="Button1_Click" Style="height: 21px" />
       <br />
        <asp:Button ID="Button2" runat="server" Text="文件下載" onclick="Button2_Click" />
    </div>
    </form>

 本文由用戶 atts 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!