Esempio n. 1
0
        public async Task <IActionResult> Upload(uploadFile data)
        {
            var result = new ResultModel();


            if (!ModelState.IsValid)
            {
                Console.WriteLine("error");
                result.IsSuccess = false;
                //result.Message = ModelState.Values();
                //return result;
                var error    = ModelState.Values.Where(a => a.Errors.Count() > 0).SelectMany(a => a.Errors).Select(a => a.ErrorMessage).ToList();
                var errorStr = error[0].ToString();
                var sb       = new StringBuilder();
                TempData["result"] = data.file.FileName + "<a href=上傳失敗>" + errorStr;
                return(RedirectToAction("ImportExcel"));
            }

            data.folder = "uploadFile";

            result = _uploadHelper.UploadData(data);
            if (result.IsSuccess)
            {
                TempData["result"] = data.file.FileName + "<a href=上傳成功>";
            }

            else
            {
                TempData["result"] = data.file.FileName + "上傳失敗";
            }
            return(RedirectToAction("ImportExcel"));
        }
Esempio n. 2
0
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <returns></returns>
        public JsonResult ExportFile()
        {
            uploadFile _uploadFile = new uploadFile();

            try
            {
                var file       = Request.Files[0]; //获取选中文件
                var filecombin = file.FileName.Split('.');
                if (file == null || string.IsNullOrEmpty(file.FileName) || file.ContentLength == 0 || filecombin.Length < 2)
                {
                    _uploadFile.code = -1;
                    _uploadFile.src  = "";
                    _uploadFile.msg  = "上传出错!请检查文件名或文件内容";
                    return(Json(_uploadFile));
                }
                //定义本地路径位置
                string localPath    = Server.MapPath("~/Upload/img/");
                string filePathName = string.Empty;

                string tmpName  = Server.MapPath("~/Upload/img/");
                var    tmp      = file.FileName;
                var    tmpIndex = 0;
                //判断是否存在相同文件名的文件 相同累加1继续判断
                while (System.IO.File.Exists(tmpName + tmp))
                {
                    tmp = filecombin[0] + "_" + ++tmpIndex + "." + filecombin[1];
                }
                //不带路径的最终文件名
                filePathName = tmp;

                if (!System.IO.Directory.Exists(localPath))
                {
                    System.IO.Directory.CreateDirectory(localPath);
                }

                file.SaveAs(Path.Combine(tmpName, filePathName));   //保存图片(文件夹)

                _uploadFile.code = 0;
                _uploadFile.src  = Path.Combine("/Upload/img/", filePathName); //name = Path.GetFileNameWithoutExtension(file.FileName),   // 获取文件名不含后缀名
                _uploadFile.msg  = "上传成功";

                //{
                //      "code": 0 //0表示成功,其它失败
                //      ,"msg": "" //提示信息 //一般上传失败后返回
                //      ,"data": {
                //                            "src": "图片路径"
                //        ,"title": "图片名称" //可选
                //      }
                //}


                return(Json(_uploadFile));
            }
            catch (Exception)
            {
                return(Json(_uploadFile, JsonRequestBehavior.AllowGet));
            }
        }
Esempio n. 3
0
        public async Task <ResultModel> UploadPhoto([FromForm] uploadPhoto data)
        {
            var result     = new ResultModel();
            var uploadFile = new uploadFile();

            uploadFile.files = data.files;
            //uploadFile.folder = _config["userPhotoUploadFolder"];
            uploadFile.folder = data.folder;
            var uploadResult = _uploadHelper.UploadData(uploadFile);

            return(uploadResult);
        }
Esempio n. 4
0
        public JsonResult Post()
        {
            uploadFile _uploadFile = new uploadFile();

            try
            {
                //获取文件
                var file = Request.Form.Files[0];
                //获取文件名称
                var filecombin = file.FileName.Split('.');
                //判断文件是否为空
                if (file == null || string.IsNullOrEmpty(file.Name) || file.Length == 0 || filecombin.Length < 2)
                {
                    _uploadFile.code = -1;
                    _uploadFile.data = new { src = "" };
                    _uploadFile.msg  = "上传错误,请上传正确信息~";
                    return(Json(_uploadFile));
                }
                string filePathName = string.Empty;
                //获取文件的绝对路径
                string loaclPath = Directory.GetCurrentDirectory() + "/wwwroot/Upload/img";
                string tempName  = Directory.GetCurrentDirectory() + "/wwwroot/Upload/img";
                //获取文件名称
                var tmp       = file.FileName;
                var tempIndex = 0;

                while (Directory.Exists(tempName + tmp))
                {
                    tmp = filecombin[0] + "_" + ++tempIndex + "." + filecombin[1];
                }
                filePathName = tmp;
                if (!Directory.Exists(loaclPath))
                {
                    Directory.CreateDirectory(loaclPath);
                }
                //写入文件 将文件复制到文件流保存
                using (var fs = new FileStream(Path.Combine(tempName, filePathName), FileMode.Create))
                {
                    fs.Flush();
                    file.CopyTo(fs);
                }
                _uploadFile.code = 0;
                _uploadFile.data = new { src = Path.Combine("/Upload/img/", filePathName) };  //name = Path.GetFileNameWithoutExtension(file.FileName),   // 获取文件名不含后缀名
                _uploadFile.msg  = "上传成功";
                return(Json(_uploadFile.data));
                //return Json("123");
            }
            catch (Exception)
            {
                return(Json(_uploadFile));
            }
        }
Esempio n. 5
0
        public ResultModel UploadData(uploadFile uploadData)
        {
            var           result    = new ResultModel();
            List <string> fileNames = new List <string>();

            if (uploadData.files.Count() <= 0)
            {
                return(null);
            }
            string path = System.IO.Path.Combine(_env.WebRootPath, uploadData.folder);

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }
            foreach (var file in uploadData.files)
            {
                var fileName = DateTime.Now.ToString("yyyyMMdd") + "_" + DateTime.Now.Ticks + Path.GetExtension(file.FileName).ToLower();

                var filePath = Path.Combine(path, fileName);
                try
                {
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        file.CopyTo(stream);
                        fileNames.Add(fileName);
                    }
                }
                catch (Exception e)
                {
                    result.IsSuccess = false;
                    result.Message   = "上傳失敗";
                    return(result);
                }
            }
            result.IsSuccess = true;
            result.Message   = "上傳成功";
            result.Data      = fileNames;
            return(result);
        }