asp.net如何实现文件的上传和下载

上传

     Boolean fileOk = false;
            //指定文件路径,pic是项目下的一个文件夹;~表示当前网页所在的文件夹
            HttpPostedFile postedFile = request.Files[0];
            string filename = DateTime.Now.ToString("yyyyMMddhhmmss");
            string filePath = "/upload/pic/" + DateTime.Now.ToString("yyyyMMdd") + "/";
            String path = HttpContext.Current.Server.MapPath(filePath);//物理文件路径
            //文件上传控件中如果已经包含文件
            string ffname = string.Empty;
            if (postedFile.ContentLength > 0)
            {
                //得到文件的后缀
                String fileExtension = Path.GetExtension(postedFile.FileName).ToLower();
                fileExtension = fileExtension.Substring(1);
                //允许文件的后缀
                String[] allowedExtensions = ConfigurationManager.AppSettings["uploadPic"].Split(',');
                //看包含的文件是否是被允许的文件的后缀
                for (int i = 0; i < allowedExtensions.Length; i++)
                {
                    if (fileExtension == allowedExtensions[i])
                    {
                        fileOk = true;
                        break;
                    }
                }
                if (fileOk == false)
                {
                    msg="{\"msg\":\"图片格式不正确!\",\"success\":false}";
                    return null;
                }
                else
                {
                    try
                    {
                        //文件另存在服务器的指定目录下
                        if (!Directory.Exists(path))
                            Directory.CreateDirectory(path);
                        ffname = filename + Path.GetExtension(postedFile.FileName);
                        postedFile.SaveAs(path + ffname);
                        return filePath + ffname;
                    }
                    catch
                    {
                        msg="{\"msg\":\"图片上传失败!\",\"success\":false}";
                        return null;
                    }
                }
            }
            return null;

 下载

   protected void Page_Load(object sender, EventArgs e)
        {
            string url = Request.QueryString["url"];  //前台传来的文件相对路径
            if (File.Exists(HttpContext.Current.Server.MapPath(url)))  //根据相对路径获取绝对路径,然后判断文件是否存在
            {
                FileInfo DownloadFile = new FileInfo(HttpContext.Current.Server.MapPath(url));  //根据绝对路径获取文件对象
                HttpContext.Current.Response.Clear();
                HttpContext.Current.Response.ClearHeaders();
                HttpContext.Current.Response.Buffer = false;
                HttpContext.Current.Response.ContentType = "application/octet-stream";
                HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(DownloadFile.Name));
                HttpContext.Current.Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());
                HttpContext.Current.Response.WriteFile(DownloadFile.FullName);
                HttpContext.Current.Response.Flush();
                HttpContext.Current.Response.End();
            }
        }

转载于:https://www.cnblogs.com/JETSh/p/5275091.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值