上传 下载

namespace 专注MVC上传下载20余年.Controllers
{
    public class modelM
    {
        public int ID { get; set; }
        public string FileNamee { get; set; }
        public string FileType { get; set; }
        public string FileNum { get; set; }
        public string FileTime { get; set; }
    }
    public class HomeController : Controller
    {
        SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=D4Text;Integrated Security=True");
        
        public ActionResult show()
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FileInfo", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var data = JsonConvert.SerializeObject(dt);
            var result = JsonConvert.DeserializeObject<List<modelM>>(data);
            con.Close();
            return View(result);
        }

        public ActionResult Upload()
        {
            if(Request.Files.Count==0)
            {
                return View();
            }
            var file = Request.Files[0];
            if (file.ContentLength == 0)
            {
                return View();
            }
            else
            {
                string target = Server.MapPath("~/Upload/");
                if(!System.IO.Directory.Exists(target))
                {
                    System.IO.Directory.CreateDirectory(target);
                }
                string filename = file.FileName;
                string path = target + filename;
                file.SaveAs(path);
                ExcelDAL exc = new ExcelDAL();
                DataTable dt = exc.ExcelToDS(path);
                foreach (DataRow dr in dt.Rows)
                {
                    modelM mm = new modelM();
                    mm.FileNamee = dr[0].ToString();
                    mm.FileType = dr[1].ToString();
                    mm.FileNum = dr[2].ToString();
                    mm.FileTime = dr[3].ToString();
                }

                modelM m = new modelM();
                m.FileNamee = filename;
                m.FileType = Path.GetExtension(filename);//获取后缀名
                m.FileNum = Request.Files.Count.ToString();
                m.FileTime = DateTime.Now.ToString();
                string sql = "INSERT INTO FileInfo VALUES('" + m.FileNamee + "','" + m.FileType + "','" + m.FileNum + "','" + m.FileTime + "')";
                con.Open();
                SqlCommand cmd = new SqlCommand(sql, con);
                var result = cmd.ExecuteNonQuery();
                con.Close();
                if(result>0)
                {
                    Response.Write("<script>alert('上传成功!');location.href='/Home/show'</script>");
                    show();
                }
            }
            return View();
        }
        public ActionResult Download(int id)
        {
            con.Open();
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM FileInfo where ID=" + id + "", con);
            DataTable dt = new DataTable();
            da.Fill(dt);
            var data = JsonConvert.SerializeObject(dt);
            var result = JsonConvert.DeserializeObject<List<modelM>>(data);
            con.Close();
            var filename = dt.Rows[0]["FileNamee"];//获取文件名
            string filepath = Server.MapPath("/upload/" + filename);//获取路径
            FileStream fs = new FileStream(filepath, FileMode.Open);//创建流转换格式
            //return File(fs, "application/vnd.ms-stream", filename.ToString());
            byte[] bytes = new byte[(int)fs.Length];//创建byte类型(int)fs.length长度
            fs.Read(bytes, 0, bytes.Length);//read
            fs.Close();
            Response.Charset = "UTF-8";
            Response.ContentType = "application/octet-stream";
            Response.ContentEncoding = Encoding.Default;
            Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            return new EmptyResult();
        }

    }
}

 

posted @ 2017-11-21 20:32  舒克老机长  阅读(210)  评论(0编辑  收藏  举报