public ActionResult Register(CreateAttachmentDto input, HttpPostedFileBase newFile, int courseId)
        {
            if (ModelState.IsValid)
            {
                var fileName = $"{input.Title.Replace(" ", string.Empty)}_{input.EmployeeId}_{DateTime.Now.ToString("ddMMyyyyhhmmsstt")}.{newFile.FileName.Split('.')[1].Trim()}";

                if (newFile != null && newFile.ContentLength > 0)
                {
                    string root = @"C:\Attachments";

                    if (!Directory.Exists(root))
                    {
                        Directory.CreateDirectory(root);
                    }

                    root = root + "\\" + fileName;

                    newFile.SaveAs(root);
                }
                input.AttachmentFileName = fileName;
                var created = _repository.Create(input);

                if (created != null)
                {
                    return(RedirectToAction("Index", "Attachments", new { employeeId = input.EmployeeId, courseId = courseId }));
                }
            }

            return(View(input));
        }
示例#2
0
        public AttachmentDto Create(CreateAttachmentDto input)
        {
            var attachment        = _mapper.Map <CreateAttachmentDto, Attachment>(input);
            var createdAttachment = _context.Attachments.Add(attachment);

            _context.SaveChanges();
            return(_mapper.Map <Attachment, AttachmentDto>(createdAttachment));
        }
示例#3
0
        public async Task Upload()
        {
            var files = Request.Form?.Files;

            if (files == null || files.Count == 0)
            {
                throw new Exception("请上传文件");
            }


            try
            {
                var realDir = Path.Combine(_webEnvironment.WebRootPath, updateDir);

                if (!Directory.Exists(realDir))
                {
                    Directory.CreateDirectory(realDir);
                }
                foreach (var file in files)
                {
                    CheckFile(file, _configuration["App:AllowExtension"], _configuration["App:MaxSize"]);


                    var attachment = new CreateAttachmentDto();

                    var fileName = file.FileName.Split('.')[0] + DateTime.Now.ToString("yyyyMMddHHmmss") + new Random().Next(1000, 9999) + Path.GetExtension(file.FileName);

                    using (var fileSteam = new FileStream(Path.Combine(realDir, fileName), FileMode.Create))
                    {
                        await file.CopyToAsync(fileSteam);
                    }



                    attachment.FileSize     = file.Length;
                    attachment.Name         = file.FileName;
                    attachment.Extenson     = Path.GetExtension(file.FileName);
                    attachment.FileName     = fileName;
                    attachment.AbsoluteUrl  = _configuration["App:ServerRootAddress"] + updateDir + "/" + fileName;
                    attachment.RelativeUrl  = "/" + updateDir + "/" + fileName;
                    attachment.AttachmentId = Guid.NewGuid().ToString("N");
                    await _attachmentAppService.CreateAsync(attachment);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }