コード例 #1
0
        public async Task <IActionResult> Edit(CreateOrEditResumeViewModel model)
        {
            var resume = await _resumeManager.FindByIdAsync(model.Id.Value);

            if (resume == null)
            {
                return(NotFound(model.Id));
            }
            if (ModelState.IsValid)
            {
                try
                {
                    model.Tags = new List <ResumeTagDto>();
                    foreach (string key in Request.Form.Keys)
                    {
                        if (key.StartsWith("Tag.", StringComparison.Ordinal) && Request.Form[key] == "on")
                        {
                            string tagValue = key.Substring("Tag.".Length);
                            model.Tags.Add(new ResumeTagDto()
                            {
                                Value = tagValue
                            });
                        }
                    }

                    _ = Mapper.Map(model, resume);

                    resume.KeyMaps = new List <ResumeKeywordMap>();
                    if (!string.IsNullOrEmpty(model.Keywords))
                    {
                        var keywords = model.Keywords.Split(' ', StringSplitOptions.RemoveEmptyEntries);
                        foreach (var keyword in keywords)
                        {
                            resume.KeyMaps.Add(new ResumeKeywordMap()
                            {
                                Keyword = keyword,
                                Name    = model.Name
                            });
                        }
                    }
                    if (Request.Form.Files != null && Request.Form.Files.Count > 0)
                    {
                        try
                        {
                            var dirPath = $"{_webEnvironment.WebRootPath}/upload/resume-attachments/{DateTime.Now:yyyy-MM-dd}";
                            if (!Directory.Exists(dirPath))
                            {
                                Directory.CreateDirectory(dirPath);
                            }

                            var attachments = new List <ResumeAttachment>();
                            foreach (var file in Request.Form.Files)
                            {
                                var oldFileName       = file.FileName;
                                var fileExtensionName = oldFileName.Substring(oldFileName.LastIndexOf(".") + 1);
                                var fileName          = $"{DateTime.Now:yyyyMMddHHmmssff}{ new Random().Next(10000, 99999) }.{fileExtensionName}";
                                //存储路径
                                var filePath = $"{dirPath}/{fileName}";
                                using (Stream stream = file.OpenReadStream())
                                {
                                    using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                                    {
                                        int    size   = 1024;
                                        byte[] buffer = new byte[size];
                                        int    length;
                                        while ((length = stream.Read(buffer, 0, size)) > 0)
                                        {
                                            fileStream.Write(buffer);
                                        }
                                    }
                                }
                                attachments.Add(new ResumeAttachment()
                                {
                                    FileName = oldFileName,
                                    FilePath = $"/upload/resume-attachments/{DateTime.Now:yyyy-MM-dd}/{fileName}"
                                });
                            }
                            await _resumeManager.AddAttachmentAsync(resume, attachments);
                        }
                        catch
                        {
                            Notifier.Error("上传附件操作失败。");
                        }
                    }
                    resume = await _resumeManager.UpdateAsync(resume, model.IgnoreSimilarity);

                    Notifier.Success("你已成功编辑了一条简历记录。");
                    return(RedirectToAction(nameof(List)));
                }
                catch (Exception ex)
                {
                    Notifier.Warning(ex.Message);
                    model.ResumeCompares = resume.ResumeCompares
                                           .Select(s => new ResumeCompareDto()
                    {
                        Similarity         = s.Similarity,
                        RelationResumeName = s.RelationResumeName,
                        RelationResumeId   = s.RelationResumeId
                    }).ToList();
                    model.OwnerUserId = resume.OwnerUserId;
                }
            }
            return(await BuildCreateOrEditDisplayAsync(model));
        }
コード例 #2
0
        public async Task <IActionResult> UploadAttachment(UploadAttachmentViewModel model)
        {
            if (ModelState.IsValid)
            {
                var resume = await _resumeManager.FindByIdAsync(model.Id);

                if (resume == null)
                {
                    return(NotFound(model.Id));
                }

                if (Request.Form.Files != null && Request.Form.Files.Count > 0)
                {
                    try
                    {
                        var webRootPath = _environment.WebRootPath;
                        var dirPath     = $"{webRootPath}/upload/resume-attachments";
                        if (!Directory.Exists(dirPath))
                        {
                            Directory.CreateDirectory(dirPath);
                        }

                        var attachments = new List <ResumeAttachment>();
                        foreach (var file in Request.Form.Files)
                        {
                            var oldFileName       = file.FileName;
                            var fileExtensionName = oldFileName.Substring(oldFileName.LastIndexOf(".") + 1);
                            var fileName          = $"{DateTime.Now:yyyyMMddHHmmssff}{ new Random().Next(10000, 99999) }.{fileExtensionName}";
                            //存储路径
                            var filePath = $"{dirPath}/{fileName}";
                            using (Stream stream = file.OpenReadStream())
                            {
                                using (FileStream fileStream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
                                {
                                    int    size   = 1024;
                                    byte[] buffer = new byte[size];
                                    int    length;
                                    while ((length = stream.Read(buffer, 0, size)) > 0)
                                    {
                                        fileStream.Write(buffer);
                                    }
                                }
                            }
                            attachments.Add(new ResumeAttachment()
                            {
                                FileName = oldFileName,
                                FilePath = $"/upload/resume-attachments/{fileName}"
                            });
                        }
                        await _resumeManager.AddAttachmentAsync(resume, attachments);

                        Notifier.Success($"你已成功上传了{Request.Form.Files.Count}条简历附件记录。");
                        return(RedirectToAction(nameof(UploadAttachment), new { Id = model.Id }));
                    }
                    catch
                    {
                        Notifier.Error("上传附件操作失败。");
                    }
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "请选择需要上传的附件文件。");
                }
            }
            return(View(model));
        }