doukengsha9472 2014-10-28 15:00
浏览 17
已采纳

通过PHP格式下载文件

I'm trying to dowloand json data by form PHP. File is downloading but dsnt get name which one i defined, also in file print only two characters from my json data.

File inside looks like:

[{

When json data looks:

[{"SIZE":[16,16]}]

He is code:

var dataAsText = JSON.stringify(data); 
var filename=$("#menu-save-text").val();
var _content = dataAsText;
jQuery('<form action="download.php" method="POST"><input type="hidden" name="filename" value="'+filename +'" /><input type="hidden" name="content" value="'+_content+'" /></form>').appendTo('body').submit().remove();

PHP:

<?
            $filename=$POST["filename"];
            header("Content-type: text/plain"); 
            header("Content-Disposition: attachment; filename=".$filename.""); 
            header("Pragma: public");
            header("Expires: 0");
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
            header("Cache-Control: private",false);

            echo $_POST["content"];        
    ?>

Downloaded files name is download.php

  • 写回答

1条回答 默认 最新

  • dongzou3751 2014-10-28 15:05
    关注

    You're suffering from an HTML injection vulnerability. The " in the JSON is breaking your HTML:

    e.g. the html you build looks like this:

    [..snip..]<input type="hidden" name="content" value="[{"SIZE":[16,16]}]" />[..snip..]
    

    Which will be parsed by the browser as:

    input:
      value="[}"         // proper attribute
      size":[16,etc...  // unknown/illegal html attribute
    

    In other words, you need to quote your JSON for usage in an html form attribute, e.g. change all the " to &quot;. That or build the HTML using proper dom methods, and set the content input's value via $(...).val(json_goes_here)-type operations with jquery.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 Oracle中如何从clob类型截取特定字符串后面的字符
  • ¥15 想通过pywinauto自动电机应用程序按钮,但是找不到应用程序按钮信息
  • ¥15 如何在炒股软件中,爬到我想看的日k线
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序