我在我的 jsp 文件中创建了一个虚拟图像。
<img id="image" style="max-width:100%;cursor:pointer;" onclick="editDiagram(this); " src="data:image/png;base64,iVBORw0K.....>
单击虚拟图像时,会调用一个 .js 函数 editDiagram,该函数会在 iFrame 中打开 draw.io 并提供制作新图像并保存它的选项。
function editDiagram(image)
{
var initial = image.getAttribute('src');
image.setAttribute('src', 'http://www.draw.io/images/ajax-loader.gif');
var iframe = document.createElement('iframe');
iframe.setAttribute('frameborder', '0');
var close = function()
{
image.setAttribute('src', initial);
document.body.removeChild(iframe);
window.removeEventListener('message', receive);
};
var receive = function(evt)
{
if (evt.data.length > 0)
{
var msg = JSON.parse(evt.data);
if (msg.event == 'init')
{
iframe.contentWindow.postMessage(JSON.stringify({action: 'load',
xmlpng: initial}), '*');
}
else if (msg.event == 'export')
{
close();
image.setAttribute('src', msg.data);
save(location.href);
}
else if (msg.event == 'save')
{
iframe.contentWindow.postMessage(JSON.stringify({action: 'export',
format: 'xmlpng', spin: 'Updating page'}), '*');
}
else if (msg.event == 'exit')
{
close();
}
}
};
window.addEventListener('message', receive);
iframe.setAttribute('src', 'https://www.draw.io/?embed=1&ui=atlas&spin=1&modified=unsavedChanges&proto=json');
document.body.appendChild(iframe);
};
function save(url)
{
if (url != null)
{
var req = new XMLHttpRequest();
req.withCredentials = true;
req.onreadystatechange = function()
{
if (req.readyState == 4)
{
if (req.status < 200 || req.status > 299)
{
alert('Error ' + req.status);
}
}
};
req.open('PUT', url, true);
req.send(document.documentElement.outerHTML);
}
}
但是当我创建新图像并单击保存时,新图像不会显示在网页中,而只会显示触发 .js 文件的虚拟图像。如何在我的网站中显示新图像并替换虚拟图像并将其保存在数据库中?