主攻ASP.NET.3.5.MVC3.0架构之重生:文件上传(十二)

 

主攻ASP.NET.3.5.MVC3.0架构之重生:文件上传(十二)

 

        public ActionResult FileUpload()

        {

            string filetype = "UpFile";

            var file = this.Request.Files[0];

            string savedFileName = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "UpFiles");

            savedFileName = System.IO.Path.Combine(savedFileName, filetype);

            string filename = DateTime.Now.ToString("yyyyMMddHHmmssfff") + "." + file.FileName.Substring(file.FileName.LastIndexOf(".") + 1);

            savedFileName = System.IO.Path.Combine(savedFileName, System.IO.Path.GetFileName(filename));

            file.SaveAs(savedFileName);



            string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录

            string fileurl = savedFileName.Replace(tmpRootDir, ""); //转换成相对路径

            fileurl = "/" + fileurl.Replace(@"\", @"/");

            

            return Json(new { success = true },"text/html");

        }

 

 代码下载: http://files.cnblogs.com/cube/UpLoad.rar

 

你可能感兴趣的:(asp.net)