Example of Visio in ASP.NET MVC Diagram Control
Visio: open a Visio drawing (.vsdx). The server reads it (its first page: shapes, their text and colours, and the connectors between them) and the diagram shows it. Try the sample drawing, or a file of your own (up to 10 MB).
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p>
Visio: open a Visio drawing (.vsdx). The server reads it (its first page: shapes, their text and colours, and the connectors between
them) and the diagram shows it. Try the sample drawing, or a file of your own (up to 10 MB).
</p>
<div class="d-flex flex-wrap gap-2 align-items-center mb-2">
<button type="button" class="btn btn-sm btn-outline-primary" id="openSample">Open the sample drawing</button>
<label class="btn btn-sm btn-outline-primary mb-0" for="visioFile">Open a Visio file</label>
<input type="file" id="visioFile" accept=".vsdx" class="visually-hidden">
<a class="btn btn-sm btn-link" href="@Url.Action("VisioSample", "DiagramDemo")">Download the sample drawing</a>
</div>
@Html.UiDiagram((UiDiagramOptions)ViewData["diagram"])
<p class="small text-muted mt-2" id="diagramStatus" role="status" aria-live="polite"></p>
<script>
window.DiagramSamples = (function () {
'use strict';
function status(text) {
var el = document.getElementById('diagramStatus');
if (el) { el.textContent = text; }
}
/** Runs fn once the component has started. */
function whenReady(id, fn) {
var c = window.Lumen && Lumen.get(id);
if (c) { fn(c); } else { window.setTimeout(function () { whenReady(id, fn); }, 50); }
}
var ID = 'visioDiagram';
/** The drawing goes to the server, which reads it; the diagram shows what comes back (or the reason it was refused). */
function open(file, name) {
var data = new FormData();
data.append('file', file, name);
var token = Lumen._antiForgeryToken();
status('Opening ' + name + '...');
fetch('/DiagramDemo/ImportVisio', { method: 'POST', headers: { RequestVerificationToken: token }, body: data })
.then(function (r) { return r.json().then(function (body) { return { ok: r.ok, body: body }; }); })
.then(function (r) {
if (!r.ok) { status(name + ' was not opened: ' + r.body.error); return; }
var d = Lumen.get(ID);
d.loadDiagram(r.body.diagram);
d.fitToPage({ margin: 20 });
status('Opened ' + name + ': ' + d.nodes.length + ' shapes and ' + d.connectors.length + ' connectors.');
})
.catch(function () { status(name + ' could not be sent.'); });
}
document.addEventListener('click', function (e) {
if (!e.target || e.target.id !== 'openSample') { return; }
fetch('/DiagramDemo/VisioSample').then(function (r) { return r.blob(); }).then(function (blob) { open(blob, 'order-process.vsdx'); });
});
document.addEventListener('change', function (e) {
if (!e.target || e.target.id !== 'visioFile' || !e.target.files.length) { return; }
var file = e.target.files[0];
e.target.value = '';
if (file.size > 10 * 1024 * 1024) { status(file.name + ' is larger than 10 MB.'); return; }
open(file, file.name);
});
return { open: open };
})();
</script>
using System;
using System.Collections.Generic;
using System.IO;
using System.Web;
using System.Linq;
using System.Web.Mvc;
using Lumen.UI.Mvc;
namespace YourApp.Controllers
{
public class DiagramController : Controller
{
// The page is Views/Diagram/Visio.cshtml; it draws what ViewData holds.
public ActionResult Visio()
{
ViewData["diagram"] = new UiDiagramOptions { Id = "visioDiagram", Height = "560px", AriaLabel = "The Visio drawing" };
return View();
}
/// <summary>
/// The Visio sample's action (as Syncfusion's): a .vsdx file in (multipart, field "file"), { diagram } out (the diagram's JSON for
/// loadDiagram); or 400 with { error } for a file that is too large, damaged, not a drawing, or unsafe to open.
/// </summary>
[HttpPost]
[ValidateJsonAntiForgeryToken]
public ActionResult ImportVisio(HttpPostedFileBase file)
{
string json, error;
if (file == null || file.ContentLength <= 0) { error = "No file was sent."; }
else if (UiVisioReader.TryReadJson(file.InputStream, out json, out error))
{
// A drawing of thousands of shapes is more than Json()'s 2 MB; the reader's limits bound it.
return new JsonResult { Data = new { diagram = json }, MaxJsonLength = int.MaxValue };
}
Response.StatusCode = 400;
Response.TrySkipIisCustomErrors = true;
return Json(new { error });
}
/// <summary>The Visio sample's drawing, to open or to download.</summary>
public ActionResult VisioSample()
{
return File(Server.MapPath("~/Content/diagram/order-process.vsdx"), "application/vnd.ms-visio.drawing", "order-process.vsdx");
}
}
}
@* Install once: Install-Package Lumen.UI.Mvc (or: dotnet add package Lumen.UI.Mvc) *@
@* Views/Web.config: <add namespace="Lumen.UI.Mvc" /> *@
@* Views/Shared/_Layout.cshtml *@
<head>
@Html.UiStyles()
<link rel="stylesheet" href="~/Content/site.css" />
</head>
<body>
@RenderBody()
@Html.UiScripts()
@RenderSection("scripts", required: false)
</body>
@* Diagrams with a DataUrl: @Html.AntiForgeryToken() on the page and [HttpPost, ValidateJsonAntiForgeryToken] on the action. *@