我正在使用this方法在我的网络应用程序的每个链接附近显示一个图标。
为了避免IE历史记录缓存问题,我将链接显示为
<a href="path/to/the/file.xls?timestamp=0234562">FileName.xls</a>
.
在这种情况下,css规则不能完成他的工作。
你知道我该如何解决这个问题吗?
答案 0 :(得分:4)
您可能使用的选择器a[href$='.xls']
仅在与HREF值的末尾匹配时才适用。请改用a[href*=.xls]
。
[att*=val]
表示具有
att
属性的元素,其值包含at 子串的至少一个实例 “VAL”。如果“val”是空字符串 然后选择器不代表 任何东西。
修改强>
如果您可以控制.htaccess文件,则可以确保避免缓存问题,因此您可以使用原始样式表规则。有关详细信息,请参阅Cache-Control Headers using Apache and .htaccess。
答案 1 :(得分:1)
问题是a[href$='.xls']
匹配锚点href
属性的结尾,但是你要附加时间戳,因此该href的结尾实际上是时间戳。
为避免缓存问题,您可以使用代理处理下载;也就是说,使用触发文件下载的脚本。在PHP中,它很容易通过readfile()函数和发送no-cache头来实现,例如:
<a href="download.php?file=spreadsheet.xls">spreadsheet.xls</a>
但是由于我不知道你使用的编程语言,我不能说更多。
答案 2 :(得分:0)
Duncan,我知道这已经得到了解答,但仅仅是为了你的评论,这里有一个适合你的功能。我认为它几乎完全取决于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);
}
}