使用css规则的文件类型图标

使用css规则的文件类型图标,css,history,icons,Css,History,Icons,我正在使用这种方法在web应用程序中指向文件的每个链接附近显示一个图标 为了避免IE历史缓存问题,我将链接显示为 <a href="path/to/the/file.xls?timestamp=0234562">FileName.xls</a>. 。在这种情况下,css规则不起作用 您知道如何解决此问题吗?您可能使用的选择器a[href$='.xls']仅在与href的值结尾匹配时应用。改用a[href*=.xls] 摘录自: 表示具有att属性且其值包含at的元素 子字

我正在使用这种方法在web应用程序中指向文件的每个链接附近显示一个图标

为了避免IE历史缓存问题,我将链接显示为
<a href="path/to/the/file.xls?timestamp=0234562">FileName.xls</a>
.
。在这种情况下,css规则不起作用


您知道如何解决此问题吗?

您可能使用的选择器
a[href$='.xls']
仅在与href的值结尾匹配时应用。改用
a[href*=.xls]

摘录自:

表示具有
att
属性且其值包含at的元素 子字符串的至少一个实例 “瓦尔”。如果“val”是空字符串 那么选择器不代表 什么都行

编辑


如果您可以控制.htaccess文件,则可以确保存在该文件以避免缓存问题,从而可以使用原始样式表规则。有关更多详细信息,请参阅。

您可能使用的选择器
a[href$='.xls']
仅在与href的值结尾匹配时才适用。改用
a[href*=.xls]

摘录自:

表示具有
att
属性且其值包含at的元素 子字符串的至少一个实例 “瓦尔”。如果“val”是空字符串 那么选择器不代表 什么都行

编辑


如果您可以控制.htaccess文件,则可以确保存在该文件以避免缓存问题,从而可以使用原始样式表规则。请参阅以了解更多详细信息。

问题在于
a[href$='.xls']
与锚定的
href
属性的结尾匹配,但您正在添加时间戳,因此该href的结尾实际上就是时间戳

为了避免缓存问题,您可以使用代理处理下载;也就是说,使用触发文件下载的脚本。在PHP中,readfile()函数很容易实现,并且不发送缓存头,例如:

<a href="download.php?file=spreadsheet.xls">spreadsheet.xls</a>


但是,因为我不知道您使用的是哪种编程语言,所以我不能再多说了。

问题是
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);
    }
}

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);
    }
}

但这也会匹配“file.xls.zip”,我认为它们的图标不会相同…@Seb在这种情况下,您可以编写
href*=.xls?
@Torok:no,因为这会匹配任何包含“.xls”的字符串,就像我给出的示例一样。@Seb:我不明白为什么。结尾的引号不允许使用其他模式。你仍然可以写
href*=.xls?timestamp=
。但是这也会匹配“file.xls.zip”,我不认为它们的图标会是一样的…@Seb在这种情况下,你可以写
href*=.xls?
@Torok:不,因为这会匹配任何包含“.xls”的字符串,就像我给出的例子一样。@Seb:我不明白为什么。结尾的引号不允许使用其他模式。您仍然可以编写
href*=.xls?timestamp=
。我正在使用php。你能解释一下你的方法吗?我用的是php。你能解释一下你的方法吗?