我想为jquery ajax响应下载pdf文件.Ajax响应包含pdf文件数据.我试过这个解决方案.我的代码如下,但我总是得到一个空白的pdf.
$(document).on('click', '.download-ss-btn', function () {
$.ajax({
type: "POST",
url: 'http://127.0.0.1:8080/utils/json/pdfGen',
data: {
data: JSON.stringify(jsonData)
}
}).done(function (data) {
var blob = new Blob([data]);
var link = document.createElement('a');
link.href = window.URL.createObjectURL(blob);
link.download = "Sample.pdf";
link.click();
});
});
Run Code Online (Sandbox Code Playgroud) 我正在使用maatwebsite/excel库来创建excel文件,然后下载我的文件
在我的控制器中我喜欢这样:
public function todas()
{
$input = Input::all();
if(isset($input['todos'])&&($input['todos']!=0))
{
set_time_limit(0);
$datainicio = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_inicio');
$datafinal = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('periodo_fim');
$mes = DB::table('tb_periodo')->where('cod', $input['todos'])->pluck('mes_referencia');
$horarioQuery = $this->horario->with(array('funcionario', 'item_contabil'))
->whereBetween('data', array($datainicio, $datafinal))
->whereNull('deleted_at')
->orderBy('cod_funcionario')
->orderBy('data', 'ASC')
->get();
$horarios = reset($horarioQuery);
$count = count($horarios);
if($horarios==null)
return Redirect::route('relatorios.todas')->withErrors(['não existem marcações para este período']);
$funcionarios = $this->funcionario->all();
$datainicio = Carbon::createFromFormat('Y-m-d H:i:s', $datainicio);
$datafinal = Carbon::createFromFormat('Y-m-d H:i:s', $datafinal);
$nome = 'Marcações'.$mes.'-'.Carbon::now()->year;
$this->horario->allToExcel($nome, $horarios);
return View::make('relatorios.todashow', compact('funcionarios', 'datainicio', 'datafinal', 'horarios', 'count', 'nome'));
}
else
{
return …Run Code Online (Sandbox Code Playgroud) 可能重复:
在新窗口中重定向
请不要介意这是愚蠢的问题,
是否有可能在PHP中使用新标签打开一个窗口
header(Location: 'blahblah');
Run Code Online (Sandbox Code Playgroud) 我需要在将文件发送给用户下载后运行几种方法.发生的事情是,在我向用户发送文件后,响应被中止,之后我再也无法做任何事情了response.end().
例如,这是我的示例代码:
Response.Clear();
Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
Response.ContentType = "application/pdf";
byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
Response.BinaryWrite(a);
Response.End();
StartNextMethod();
Response.Redirect(URL);
Run Code Online (Sandbox Code Playgroud)
所以,在这个例子中StartNextMethod并Response.Redirect没有执行.
我尝试的是使用以下代码创建了一个单独的处理程序(ashx):
public void ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.AddHeader("content-disposition", "attachment; filename=test.pdf");
context.Response.ContentType = "application/pdf";
byte[] a = System.Text.Encoding.UTF8.GetBytes("test");
context.Response.BinaryWrite(a);
context.Response.End();
}
Run Code Online (Sandbox Code Playgroud)
并称之为:
Download d = new Download();
d.ProcessRequest(HttpContext.Current);
StartNextMethod();
Response.Redirect(URL);
Run Code Online (Sandbox Code Playgroud)
但同样的错误发生了.我已经尝试用CompleteRequest替换Response.End,但它没有帮助.
我想问题是我正在使用HttpContext.Current但应该使用单独的响应流.那是对的吗?我如何在一个单独的方法中一般地做到这一点(假设我希望我的处理程序接受数据和内容类型的字节数组,并且可以从单独的响应中下载.我真的不想使用单独的页面来响应.
更新
我仍然没有找到一个好的解决方案.我想在用户下载文件后做一些操作,但不使用单独的页面来响应\请求.
我正在考虑允许通过PayPal从我这里购买一些内容的客户的问题.我会提供多种无形商品.当有人完成对其中一件商品的购买时,他们将被重定向到一个登陆页面 - 让我们称之为"thank_you.php" - 这将自动排队下载并允许链接排队下载以防万一自动开始.这将通过将唯一项ID传递到下载页面("download.php")来完成.
这种方法基本上是这些线程的最佳答案的模仿:
一个PHP脚本,让用户从我的网站下载文件而不泄露我网站上的实际文件链接?
但是,我担心一旦用户使用"thank_you.php",他们就可以下载他们的项目,然后使用Firebug(或等同物)编辑项目ID并下载另一个不同的项目:
<a href="download.php/38a205ec300a3874c867b9db25f47c61">Download Here</a>
Run Code Online (Sandbox Code Playgroud)
至
<a href="download.php/7c8ddc86c0e4c14517b9439c599f9957">Download Here</a>
Run Code Online (Sandbox Code Playgroud)
我需要那些比我更好的想法和帮助:我可以实现什么(以及如何)作为一种解决方案,仍然允许相同的客户访问和休闲,但阻止这种操纵?
编辑:ID哈希用于预览和引用整个网站的项目,我不担心人们猜测,而是他们在单独的选项卡中浏览网站以获取其他ID,并只是继续下载不同的项目.
好的,我有以下php文件:
<?php
header("Content-Type: application/octet-stream");
$file = $_GET["file"];
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
$file = "pdf/".$file;
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
if ($file="pdf/sampleFile.pdf") {
?>
<html>
<head>
<title>Best Recipes Ever!</title>
<link rel="stylesheet" href="style/style.css" />
</head>
<body>
<div id="header">
<h1>Best Recipes Ever!</h1>
</div>
<div id="page">
<h1>Thank You For Your …Run Code Online (Sandbox Code Playgroud)