【发布时间】:2016-06-26 09:42:22
【问题描述】:
我正在开发一个 SVG 编辑器。我必须将 svg 图片保存在本地磁盘上。如您所知,出于安全原因,直接使用 javascript 是不可能的。所以我决定用服务器端的帮助来解决这个问题。我在一个名为“savefile.php”的文件中编写了以下 PHP 例程:
<?php
$mime = array('xls' => 'application/vnd.ms-excel', 'xml' => 'application/xml', 'html' => 'text/html', 'cvs' => 'text/plain', 'svg' => 'text/plain', 'txt' => 'text/plain', 'json' => 'text/plain', 'array' => 'text/plain');
if (isset($_POST['format']) && isset($_POST['filename']) && isset($_POST['content'])) {
$filename = $_POST['filename'];
$format = $_POST['format'];
$content = $_POST['content'];
$fullName = $filename . '.' . $format;
header('Pragma: public');
header("Content-Description: File Transfer");
header("Content-Transfer-Encoding: binary");
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header("Cache-Control: public");
header('Content-Type: ' . $mime[$format]);
header('Content-Disposition: attachment; filename="' . basename($fullName) . '"');
echo $content;
}
?>
在我调用的服务器端,PHP 过程,代码如下:
var obj = {
format: 'svg',
filename: 'myfilename',
content: glb.Draw_active().GetDisegno().svg()
};
$.post("php/savefile.php",obj,function(data,status,xhr){console.log('Executed');});
当软件执行上述代码时,浏览器应打开保存文件窗口并等待用户确认.....但没有任何反应。我确定执行了 PHP 例程,它也执行了回调例程,但仅此而已。 出了点问题,我的嫌疑人在 javascript 客户端代码上,但我找不到任何文档。可能有人对此程序有经验吗?
非常感谢。
【问题讨论】:
-
感谢您的回答,我把那个问题加红了,但是,可能是因为我是这方面的新手,我不明白我的代码有什么问题。可能你能帮助我吗?谢谢
-
“我不明白我的代码有什么问题”——您正在使用 Ajax。这就是问题所在。
标签: javascript php svg server client