javascript - 使用 PHP header 将文件保存在本地电脑中

标签 javascript php svg server client

我正在开发一个 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 客户端代码上,但我找不到任何文档。也许有人有此过程的经验?

非常感谢。

最佳答案

感谢@Quentin 的帮助,我已经解决了问题。上面的 PHP 代码可以工作,我必须更改 Javascript 部分。这是工作代码:

保存文件例程:

/*
    @datatype       : 'svg', 'xml', 'txt', 'xls', 'csv', 'html', etc see the list in PHP file
    @filename       : filename without extension
    @exportServer   : name of the PHP file on the server
    @content        : content of the file to save
*/
var saveFile = function(datatype, filename, exportServer,content){
    var HInput = function(value, name, form) {
        var I = document.createElement('input');
        I.name = name;
        I.value = value;
        I.type = 'hidden';
        form.appendChild(I);
        },
    HTextArea = function(value, name, form) {
         var tA = document.createElement('textarea');
         tA.name = name;
         tA.value = value;
         form.appendChild(tA);
         };
    var form = document.createElement('form');
    HInput(filename, 'filename', form);
    HInput(format, 'format', form);
    HTextArea(content, 'content', form);
    form.action = exportServer;
    form.method = 'post';
    document.body.appendChild(form);
    form.submit();
    document.body.removeChild(form);
    }

....根据我的需要,我这样调用它:

saveFile('svg','myfilename',"php/savefile.php",data);

仅此而已......;)

关于javascript - 使用 PHP header 将文件保存在本地电脑中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35935242/

相关文章:

javascript - 如何迭代 JSON 中键值的数组

javascript - php从查询字符串中删除单个变量值对

javascript - 如何找到 SVG 中最接近的元素?

javascript - 图像在 Firefox 中损坏或 chop

javascript - 在底部滚动

javascript - moment-duration-format.d.ts 定义不扩展力矩模块

php检查gravatar是否存在header错误

php - Sql 查询查找具有特定最后状态的订单

javascript - d3js 堆叠条形图中的分页

javascript - 如何更改innerHTML的innerHTML