Пример #1
0
        public async Task <IActionResult> Edit(int id, [Bind("CaseAttachmentID,CaseID,LocalUserID,FilePath,AttachmentTimestamp")] CaseAttachment caseAttachment)
        {
            if (id != caseAttachment.CaseAttachmentID)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    _context.Update(caseAttachment);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!CaseAttachmentExists(caseAttachment.CaseAttachmentID))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }
                return(RedirectToAction(nameof(Index)));
            }
            ViewData["CaseID"]      = new SelectList(_context.Case, "CaseID", "CaseID", caseAttachment.CaseID);
            ViewData["LocalUserID"] = new SelectList(_context.LocalUser, "LocalUserID", "LocalUserID", caseAttachment.LocalUserID);
            return(View(caseAttachment));
        }
Пример #2
0
        public async Task <IActionResult> Create(int id, CaseAttachmentCreateViewModel model)
        {
            if (id == null)
            {
                return(NotFound());
            }
            if (ModelState.IsValid)
            {
                string uniqueFileName = null;
                if (model.Attachments != null && model.Attachments.Count > 0)
                {
                    string uFolder = Path.Combine(hostingEnvironment.WebRootPath, "Attachments");
                    //if (!Directory.Exists(uFolder))
                    //{
                    //    Directory.CreateDirectory(uFolder);
                    //}

                    foreach (IFormFile Attachment in model.Attachments)
                    {
                        string uploadsFolder = Path.Combine(hostingEnvironment.WebRootPath, "Attachments");
                        uniqueFileName = Guid.NewGuid().ToString() + "_" + Attachment.FileName;
                        //uniqueFileName = Attachment.FileName;
                        string filePath = Path.Combine(uploadsFolder, uniqueFileName);
                        // Use CopyTo() method provided by IFormFile interface to
                        // copy the file to wwwroot/images folder
                        FileStream file_stream = new FileStream(filePath, FileMode.Create, FileAccess.Write);
                        Attachment.CopyTo(file_stream);
                        file_stream.Close();
                        file_stream.Dispose();
                        CaseAttachment newAttachment = new CaseAttachment
                        {
                            LocalUserID = User.Identity.Name,
                            CaseID      = id,
                            // Store the file name in PhotoPath property of the employee object
                            // which gets saved to the Employees database table
                            FilePath = uniqueFileName,
                            FileName = Attachment.FileName
                        };
                        _context.Add(newAttachment);
                    }

                    await _context.SaveChangesAsync();
                }
                int cid = id;
                return(RedirectToAction("Details", "Cases", new { id = cid }));
            }
            ViewData["CaseID"] = new SelectList(_context.Case, "CaseID", "CaseID", model.CaseID);
            //ViewData["LocalUserID"] = new SelectList(_context.LocalUser, "LocalUserID", "LocalUserID", caseAttachment.LocalUserID);
            return(View());
        }