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" οnchange="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="文件下载" οnclick="Button2_Click" />
    </div>
    </form>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值