예제 #1
0
        public async Task <IActionResult> Create([Bind("ID, DocumentName, Description")] SvgDocument svgDocument, IFormFile file)
        {
            if (file != null)
            {
                string extension = Path.GetExtension(file.FileName);
                if (extension.ToLower() != ".svg")
                {
                    throw new FormatException("Invalid file extension!");
                }
                string localPath  = "/documents/" + file.FileName;
                string globalPath = _hostingEnvironment.WebRootPath + localPath;
                svgDocument.DocumentPath = localPath;

                if (ModelState.IsValid)
                {
                    _context.Add(svgDocument);
                    await _context.SaveChangesAsync();
                }
                else
                {
                    return(View(svgDocument));
                }

                using (FileStream fs = new FileStream(globalPath, FileMode.Create))
                {
                    await file.CopyToAsync(fs);
                }

                XmlDocument doc = new XmlDocument();
                doc.Load(globalPath);
                var editableElements = SvgEditor.FindEditableElements(doc, "text", "id");

                for (int i = 0; i < editableElements.Count; i++)
                {
                    SvgElement element = new SvgElement()
                    {
                        DocumentId         = svgDocument.ID,
                        AttributeName      = editableElements[i].Attributes["id"].Value,
                        AttributeInnerText = editableElements[i].InnerText,
                        IsActive           = true
                    };
                    _context.SvgElements.Add(element);
                }
                _context.SaveChanges();

                return(RedirectToAction(nameof(Index)));
            }

            return(View(svgDocument));
        }
예제 #2
0
        public async Task <IActionResult> Create([Bind("Id,DocumentId,AttributeName,AttributeInnerText,IsActive")] SvgElement svgElement)
        {
            if (!User.Identity.IsAuthenticated)
            {
                return(Forbid());
            }
            if (ModelState.IsValid)
            {
                _context.Add(svgElement);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(svgElement));
        }