Added file saving/export ability to server_opensave extension, fixing issue 582
git-svn-id: http://svg-edit.googlecode.com/svn/trunk@1625 eee81c28-f429-11dd-99c0-75d572ba1dddmaster
parent
c0fa1ef4b1
commit
8ce73cd5ca
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* ext-server_open.js
|
||||
* ext-server_opensave.js
|
||||
*
|
||||
* Licensed under the Apache License, Version 2
|
||||
*
|
||||
|
@ -7,8 +7,73 @@
|
|||
*
|
||||
*/
|
||||
|
||||
svgEditor.addExtension("server_open", {
|
||||
svgEditor.addExtension("server_opensave", {
|
||||
callback: function() {
|
||||
|
||||
var save_svg_action = 'extensions/filesave.php';
|
||||
var save_png_action = 'extensions/filesave.php';
|
||||
|
||||
// Create upload target (hidden iframe)
|
||||
var target = $('<iframe name="output_frame" src="#"/>').hide().appendTo('body');
|
||||
|
||||
svgEditor.setCustomHandlers({
|
||||
save: function(win, data) {
|
||||
var svg = "<?xml version='1.0'?>\n" + data;
|
||||
|
||||
var title = svgCanvas.getDocumentTitle();
|
||||
var filename = title.replace(/[^a-z0-9\.\_\-]+/gi, '_');
|
||||
|
||||
var form = $('<form>').attr({
|
||||
method: 'post',
|
||||
action: save_svg_action,
|
||||
target: 'output_frame'
|
||||
}) .append('<input type="hidden" name="output_svg" value="' + encodeURI(svg) + '">')
|
||||
.append('<input type="hidden" name="filename" value="' + filename + '">')
|
||||
.appendTo('body')
|
||||
.submit().remove();
|
||||
},
|
||||
pngsave: function(win, data) {
|
||||
var issues = data.issues;
|
||||
|
||||
if(!$('#export_canvas').length) {
|
||||
$('<canvas>', {id: 'export_canvas'}).hide().appendTo('body');
|
||||
}
|
||||
var c = $('#export_canvas')[0];
|
||||
|
||||
c.width = svgCanvas.contentW;
|
||||
c.height = svgCanvas.contentH;
|
||||
canvg(c, data.svg);
|
||||
var datauri = c.toDataURL('image/png');
|
||||
|
||||
var uiStrings = svgEditor.uiStrings;
|
||||
var note = '';
|
||||
|
||||
// Check if there's issues
|
||||
if(issues.length) {
|
||||
var pre = "\n \u2022 ";
|
||||
note += ("\n\n" + pre + issues.join(pre));
|
||||
}
|
||||
|
||||
if(note.length) {
|
||||
alert(note);
|
||||
}
|
||||
|
||||
var title = svgCanvas.getDocumentTitle();
|
||||
var filename = title.replace(/[^a-z0-9\.\_\-]+/gi, '_');
|
||||
|
||||
var form = $('<form>').attr({
|
||||
method: 'post',
|
||||
action: save_png_action,
|
||||
target: 'output_frame'
|
||||
}) .append('<input type="hidden" name="output_png" value="' + datauri + '">')
|
||||
.append('<input type="hidden" name="filename" value="' + filename + '">')
|
||||
.appendTo('body')
|
||||
.submit().remove();
|
||||
|
||||
|
||||
}
|
||||
});
|
||||
|
||||
// Do nothing if client support is found
|
||||
if(window.FileReader) return;
|
||||
|
||||
|
@ -54,7 +119,7 @@ svgEditor.addExtension("server_open", {
|
|||
enctype: 'multipart/form-data',
|
||||
method: 'post',
|
||||
action: open_svg_action,
|
||||
target: 'upload_target'
|
||||
target: 'output_frame'
|
||||
});
|
||||
|
||||
// Create import form
|
||||
|
@ -105,9 +170,6 @@ svgEditor.addExtension("server_open", {
|
|||
rebuildInput(import_svg_form);
|
||||
rebuildInput(import_img_form);
|
||||
|
||||
// Create upload target (hidden iframe)
|
||||
var target = $('<iframe name="upload_target" src="#"/>').hide().appendTo('body');
|
||||
|
||||
// Add forms to buttons
|
||||
$("#tool_open").show().prepend(open_svg_form);
|
||||
$("#tool_import").show().prepend(import_svg_form);
|
|
@ -1,5 +1,14 @@
|
|||
<!doctype html>
|
||||
<?php
|
||||
/*
|
||||
* fileopen.php
|
||||
* To be used with ext-server_opensave.js for SVG-edit
|
||||
*
|
||||
* Licensed under the Apache License, Version 2
|
||||
*
|
||||
* Copyright(c) 2010 Alexis Deveria
|
||||
*
|
||||
*/
|
||||
// Very minimal PHP file, all we do is Base64 encode the uploaded file and
|
||||
// return it to the editor
|
||||
|
||||
|
|
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
/*
|
||||
* filesave.php
|
||||
* To be used with ext-server_opensave.js for SVG-edit
|
||||
*
|
||||
* Licensed under the Apache License, Version 2
|
||||
*
|
||||
* Copyright(c) 2010 Alexis Deveria
|
||||
*
|
||||
*/
|
||||
|
||||
if(!isset($_POST['output_svg']) && !isset($_POST['output_png'])) {
|
||||
die('post fail');
|
||||
}
|
||||
|
||||
$file = '';
|
||||
|
||||
$suffix = isset($_POST['output_svg'])?'.svg':'.png';
|
||||
|
||||
if(isset($_POST['filename']) && strlen($_POST['filename']) > 0) {
|
||||
$file = $_POST['filename'] . $suffix;
|
||||
} else {
|
||||
$file = 'image' . $suffix;
|
||||
}
|
||||
|
||||
if($suffix == '.svg') {
|
||||
$mime = 'image/svg+xml';
|
||||
$contents = rawurldecode($_POST['output_svg']);
|
||||
} else {
|
||||
$mime = 'image/png';
|
||||
$contents = $_POST['output_png'];
|
||||
$pos = (strpos($contents, 'base64,') + 7);
|
||||
$contents = base64_decode(substr($contents, $pos));
|
||||
}
|
||||
|
||||
header("Cache-Control: public");
|
||||
header("Content-Description: File Transfer");
|
||||
header("Content-Disposition: attachment; filename=" . $file);
|
||||
header("Content-Type: " . $mime);
|
||||
header("Content-Transfer-Encoding: binary");
|
||||
|
||||
echo $contents;
|
||||
|
||||
?>
|
Loading…
Reference in New Issue