File type icons using css rule
I use this approach to display an icon next to each file link from my web application.
To avoid IE history cache issues, I am displaying the link as
<a href="path/to/the/file.xls?timestamp=0234562">FileName.xls</a>
... In this case, the css rule is not doing its job.
Do you know how I can solve this problem?
a source to share
The selector a[href$='.xls']
you are probably using only applies if it matches the end of the HREF value. Use instead a[href*=.xls]
.
Excerpt from Selectors Level 3 :
[att*=val]
Represents an element with an attribute
att
whose value contains at least one instance of the substring Val. If "val" is an empty string then the selector does not represent anything.
Edit
If you have control over the .htaccess file, you can enforce there to avoid cache issues, so you can use your original stylesheet rules. See Caching Headers Using Apache and .htaccess for details .
a source to share
The problem is that it a[href$='.xls']
matches the end of href
your anchor attribute , but you are adding a timestamp, so the end of that href is actually a timestamp.
To avoid caching issues, you can handle downloads using a proxy; that is, use a script that starts the file upload. In PHP, it is easily implemented with readfile () function and sends headers without cache, for example:
<a href="download.php?file=spreadsheet.xls">spreadsheet.xls</a>
But since I don't know which programming language you are using, I couldn't say much more.
a source to share
Duncan, I know this has been answered already, but just for your comment, here is a function that should work for you. I think this is almost straight from the PHP manual or some other examples. I have this in a class that handles other things like checking file permissions, uploads, etc. Modify your needs.
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);
}
}
a source to share