【问题标题】:Save file in local pc using PHP headers使用 PHP 标头将文件保存在本地 pc
【发布时间】: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


【解决方案1】:

感谢@Quentin 帮助我解决了这个问题。上面的 PHP 代码正在运行,我不得不更改 Javascript 部分。这里的工作代码:

SaveFile 例程:

/*
    @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);

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多