示例#1
0
        public async Task <ActionResult <UploadFile> > UploadFile([FromForm] List <UploadFile> entity)
        {
            if (ModelState.IsValid)
            {
                var list         = new List <UploadFile>();
                var file         = Request.Form.Files["UploadedFile"];
                var uploadBy     = Request.Form["uploadBy"];
                var actionPlanID = Request.Form["actionPlanID"];
                if (file != null)
                {
                    if (!Directory.Exists(_environment.WebRootPath + "\\FileUpload\\"))
                    {
                        Directory.CreateDirectory(_environment.WebRootPath + "\\FileUpload\\");
                    }

                    for (int i = 0; i < Request.Form.Files.Count; i++)
                    {
                        var currentFile = Request.Form.Files[i];
                        using FileStream fileStream = System.IO.File.Create(_environment.WebRootPath + "\\FileUpload\\" + currentFile.FileName);
                        await currentFile.CopyToAsync(fileStream);

                        fileStream.Flush();
                        list.Add(new UploadFile
                        {
                            ActionPlanID = actionPlanID.ToInt(),
                            UploadBy     = uploadBy.ToInt(),
                            Name         = currentFile.FileName
                        });
                    }
                }
                var model = await _actionPlanService.UploadFile(list);

                return(Ok(model));
            }
            else
            {
                var errors = ModelState.Values.SelectMany(v => v.Errors);
            }
            return(Ok(entity));
        }