我正在使用这种方法在我的网络应用程序中指向文件的每个链接附近显示一个图标。
为了避免 IE 历史缓存问题,我将链接显示为
<a href="path/to/the/file.xls?timestamp=0234562">FileName.xls</a>
.
在这种情况下,css 规则不起作用。
你知道我该如何解决这个问题吗?
您可能使用的选择器a[href$='.xls']
仅在与 HREF 值的结尾匹配时才适用。改为使用a[href*=.xls]
。
选择器级别 3的摘录:
[att*=val]
表示具有属性的元素,该
att
属性的值至少包含子字符串“val”的一个实例。如果 "val" 是空字符串,则选择器不代表任何内容。
编辑
如果您可以控制 .htaccess 文件,则可以确保避免缓存问题,因此您可以使用原始样式表规则。有关更多详细信息,请参阅使用 Apache 和 .htaccess的缓存控制标头。
问题是与锚属性a[href$='.xls']
的结尾相匹配href
,但是您要附加时间戳,因此该 href 的结尾实际上是时间戳。
为避免缓存问题,您可以使用代理处理下载;也就是说,使用触发文件下载的脚本。在 PHP 中,它很容易通过 readfile() 函数和发送无缓存标头来实现,例如:
<a href="download.php?file=spreadsheet.xls">spreadsheet.xls</a>
但是由于我不知道您使用的是哪种编程语言,所以我不能多说。
邓肯,我知道这已经得到回答,但只是为了您的评论,这里有一个适合您的功能。我认为它几乎直接来自 PHP 手册或某处的其他示例。我在一个处理其他事情(如检查文件权限、上传等)的类中有这个。根据您的需要进行修改。
public function downloadFile($filename)
{
// $this->dir is obviously the place where you've got your files
$filepath = $this->dir . '/' . $filename;
// make sure file exists
if(!is_file($filepath))
{
header('HTTP/1.0 404 Not Found');
return 0;
}
$fsize=filesize($filepath);
//set mime-type
$mimetype = '';
// mime type is not set, get from server settings
if (function_exists('finfo_file'))
{
$finfo = finfo_open(FILEINFO_MIME); // return mime type
$mimetype = finfo_file($finfo, $filepath);
finfo_close($finfo);
}
if ($mimetype == '')
{
$mimetype = "application/force-download";
}
// replace some characters so the downloaded filename is cool
$fname = preg_replace('/\//', '', $filename);
// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mimetype");
header("Content-Disposition: attachment; filename=\"$fname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
// download
$file = @fopen($filepath,"rb");
if ($file)
{
while(!feof($file))
{
print(fread($file, 1024*8));
flush();
if (connection_status()!=0)
{
@fclose($file);
die(); // not so sure if this best... :P
}
}
@fclose($file);
}
}