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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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();
            }
        }

以上是关于asp.net如何实现文件的上传和下载的主要内容,如果未能解决你的问题,请参考以下文章

ASP.net如何实现用唯一的用户名重命名上传的图片和显示图片

asp.net 上传多文件问题

js 大文件分片上传处理如何实现?

asp.net4.0中多图片上传如何操作

ASP.Net如何用FileUpLoad实现多文件上传

如何在 ASP.NET 中实现 Blueimp jQuery 文件上传