mvc控制器文件上传

文件上传方法

 public ActionResult UploadFilesReg(HttpPostedFileBase file,string cdmx)
        {
            string res = string.Empty;
            string cd = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
            string path = "C:/inetpub/VirtualDrawing/SRM/" + cd + "/" + cdmx;

            if (Directory.Exists(path) == true)
            {
                var fileName = file.FileName;
                var urlCombine = Path.Combine(path, DateTime.Now.ToString("yyyyMMddHHmmss") + '-' + fileName);
                file.SaveAs(urlCombine);
                var saveUrl = "VirtualDrawing/SRM/" + cd + "/" + cdmx + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + '-' + fileName;
                string str = fileName + ';' + saveUrl;
                res = "{\"code\":0,\"msg\":\"" + str + "\",\"count\":\"\",\"data\":\"\"}";
            }
            else
            {
                Directory.CreateDirectory(path);
                var fileName = file.FileName;
                var urlCombine = Path.Combine(path, DateTime.Now.ToString("yyyyMMddHHmmss") + '-' + fileName);
                file.SaveAs(urlCombine);
                var saveUrl = "VirtualDrawing/SRM/" + cd + "/" + cdmx + "/" + DateTime.Now.ToString("yyyyMMddHHmmss") + '-' + fileName;
                string str = fileName + ';' + saveUrl;
                res = "{\"code\":0,\"msg\":\"" + str + "\",\"count\":\"\",\"data\":\"\"}";
            };

            return Json(res);
        }

虚拟目录创建方法
IIS—>网站下—>添加虚拟目录
创建上传服务器的文件存放路径,及访问路径。

上一篇:mvc中常见的属性验证


下一篇:题目:Catch That Cow(C语言实现)