相关疑难解决方法(0)

为什么Content-Disposition标头在IE 8中不起作用?

我正在尝试将文本文件(CSV)流式传输到响应,并且以下代码在Firefox 3中完美运行,但是当我使用IE时,看起来它想要下载实际的.aspx页面,并抱怨该文件内容与文件扩展名或类型不匹配.如果我然后选择下载文件,它会正确下载CSV数据并在Excel中打开它.我究竟做错了什么?

    DataTable dt = ExtensionsProvider.ListPrivateCallCostsForCsv(reportFilter.BusinessUnit, reportFilter.StartDate,
                                                             reportFilter.EndDate);
    Response.Clear();
    Response.Buffer = true;
    Response.ContentType = "text/csv";
    Response.AddHeader("Content-Disposition", "filename=" + GetExportFileName()); 
    DataTableHelper.WriteCsv(dt, Response.Output, false);
    Response.End();
Run Code Online (Sandbox Code Playgroud)

asp.net

3
推荐指数
1
解决办法
1万
查看次数

如何在使用nginx下载时动态重命名PDF文件创建重写规则?

我正在网站上工作,我允许用户下载PDF文件.每个PDF文件使用随机哈希名称存储在服务器上,例如.

file
768E1F881783BD61583D64422797632A35B6804C.pdf 
is stored in
/usr/share/nginx/html/contents/7/6/8/E/1/768E1F881783BD61583D64422797632A35B6804C.pdf 
Run Code Online (Sandbox Code Playgroud)

现在我可以尝试给用户直接位置的文件,但下载后的文件名显示为768E1F881783BD61583D64422797632A35B6804C.pdf,我想动态重命名文件,我可以使用这样的PHP来实现这个

<?php
// We'll be outputting a PDF  
header('Content-type: application/pdf');

// It will be called downloaded.pdf  
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf  
readfile('original.pdf');
?> 
Run Code Online (Sandbox Code Playgroud)

ref:重命名pdf文件以便在飞行中下载

但我正在直接购买nginx规则,可以将下载网址重写为路径并在运行时重命名.

我怎么能这样做?

我尝试过这样的事情.

location ^/download-pdf {
    alias   /usr/share/nginx/html/contents;
if ($request_filename ~ ^.*?/[^/]*?_(.*?\..*?)$)
{
    set $filename $1;
}

add_header Content-Disposition 'attachment; filename=$filename';
}
Run Code Online (Sandbox Code Playgroud)

所以,如果我发送用户到这个位置

domain.com/download-pdf/768E1F881783BD61583D64422797632A35B6804C.pdf?title=this.is.test
Run Code Online (Sandbox Code Playgroud)

然后我希望使用标题/文件名作为this.is.test.pdf在用户PC上下载此文件

/usr/share/nginx/html/contents/7/6/8/E/1/768E1F881783BD61583D64422797632A35B6804C.pdf 
Run Code Online (Sandbox Code Playgroud)

这可以仅使用nginx重写规则来完成吗?或者我也需要使用PHP


UPDATE1:

我试过像这样使用它

location ^/download-pdf/([0-9A-F])/([0-9A-F])/([0-9A-F])/([0-9A-F])/([0-9A-F])/([0-9A-F]+).pdf$ {
    alias   /usr/share/nginx/basesite/html/contents;
add_header Content-Disposition 'attachment; …
Run Code Online (Sandbox Code Playgroud)

mod-rewrite rewrite nginx

3
推荐指数
1
解决办法
2003
查看次数

标签 统计

asp.net ×1

mod-rewrite ×1

nginx ×1

rewrite ×1