There seem to be lots of solutions out there for CodeIgniter to force download of a file, but I can't find any for outputting a file without forcing a download.
In my case, I've redirected my file downloads through a controller (originally Apache handled them directly) in order to log statistics. I used the CodeIgniter force_download function in the download helper. However, I've now had a request that some or all files should be able to be opened in the browser, such that saving to a file isn't forced, so I'm wanting to know how to do it.
Presumably, it's mostly a matter of outputting the right header, though I don't know whether different headers will be required for different file types. Has anyone got an example?
You just have to specify the good mime-type and omit Content-Disposition: attachment.
So if the browser can render it, it will be display. If it can't, it will be download.
example:
$file = 'images/smileys/panda.gif';
if (file_exists($file))
{
$finfo = new finfo(FILEINFO_MIME);
header('Content-Description: File Transfer');
header('Content-Type: '.$finfo->file($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
exit;
}
Related
I am unable to upload a .zip file from Jmeter.
Upon trying to upload the file i am getting an error like below,
{
"args":[
"unsupportedMediaType"],
"message":"Request media type is not supported",
"messageId":"unsupportedMediaType",
"correlationId":"6539cd74-5f09-473c-40d2-36f98c0a472b",
"causes":[
],
"status":415
}
HTTP Method supported: POST.
Request header:
Content-Type: application/json; charset=UTF-8
Can anyone please help me in uploading the .zip file from the Jmeter, refer to the below image for my request,
Thanks in advance.
Are you sure about your content-type and what you are doing ?
for zip content type application/json is wrong.
fileToString will try to transform byes(zip) to text which cannot work
Try
checking Use multipart/form-data for Post
and Browser compatible headers .
remove FileToString call and use last tab Files upload instead of body data. And use there the content type application/zip or something suitable
If you are trying to upload a file and send a body, you may need to use nightly build.
I am trying to use this snippet:
open("data.csv", "wb") do |file|
file << open("https://website.com/data.php", http_basic_authentication: ["username", "password"]).read
end
But instead of the desired CSV file, I get just downloaded the HTML code of the website. What's the problem?
When I access the URL and I am not logged in, then it's displayed the form for login (not the HTTP authentication window).
How to solve this situation?
Thanks
I think you should try out net/http http://dzone.com/snippets/how-download-files-ruby
It's probably because your php script return a response with a mime-type different of text/plain or better : text/csv
please see this related previous response
How to use the CSV MIME-type?
in the PHP Script :
header('Content-Type: text/csv');
header('Content-disposition: attachment;filename=data.csv');
I'm using wkhtmltopdf to generate PDF files. This program accepts one of two ContentType values, 'application/pdf' and 'application/octet-stream'. I want the PDF files generated to be saved to the user's machine, so I set it to 'application/octet-stream'. Chrome prompted the download, just as expected. So did FireFox. Of course Internet Explorer ignored this setting and opened the PDF file in a browser window instead. I'm using Internet Explorer 8 and 9 for testing this. I don't have access to the server, and I don't want to change how all PDF files are handled in any case. I want this application to prompt the user to download the file, and need to know how to force IE to do this instead of ignoring the ContentType value.
I used Fiddler2 to check out how the ContentType is being reported in IE and Chrome, and both show 'application/octet-stream', so I'm not sure why IE is insisting on rendering the PDF in a browser. Any advice?
PHP CODE:
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Pragma: public");
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=\"filename\"");
This should work.
I'm having an issue with audio tag and Firefox. I am using the default browsers' controls. It works fine with Chrome and Opera, but in Firefox progress bar stays on right(like it was the end of the record) - the sound plays normally, but I can't seek through the file. What did I do wrong?
Here's how I send my file to browser.
header('Content-Description: File Transfer');
header('Content-Type: audio/wav');
header('Content-Disposition: attachment; filename=' . basename($clientName));
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
readfile($file);
And my use of audio tag:
<audio preload="auto" controls="controls" src="/record/download/id/55" />
Thanks in advance for your answers.
Firefox will send range requests (with the "Range" request header) and your server code should respond accordingly (with "206 Partial Content" status). When the server doesn't appear to understand range request firefox will assume it's some kind of a live stream and behave in the described manner.
If your code runs through an apache server you can have it handle range requests for you, for instance by using mod_xsendfile. Another option is to implement is yourself. For details see RFC 2616, especially section 10.2.7 and 14.35.
I was trying to find a way to prevent browsers from caching PDF that is being loaded using a streaming methods.
FireFox and Chorme deals just fine with the following headers and doesn't cache any pdf file:
Response.AddHeader("Pragma", "no-cache, no-store");
Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate, max-age=0");
Response.AddHeader("Expires", "-1");
Although, IE 7 (with acrobat reader 9.4.1) works only with the following headers and prevent the caching of the PDF doc:
Response.AddHeader("Pragma", "no-cache, no-store");
Response.AddHeader("Cache-Control", "private, must-revalidate, max-age=0");
Response.AddHeader("Expires", "-1");
When i was trying to use IE 7 with Acrobat Reader 10, the above header didn't make any different and cached the PDF no matter what i tried.
When i am trying to put Cache-Control: no-cache, no-store, the pdf was not loaded at all.
According to my understanding, IE use the cache mechanism to load the PDF documents.
Is there anyone familiar with a global or specific way (by using other headers for example) that can help prevent caching of PDF documents?
Add a random number to the URL, either in the path or in the query string. That way, it'll download the file every time. You could also change the number only, if the file has changed, for example using the mtime of the file.
PHP (since everybody understands that, even if nobody likes it):
Download PDF
This issue of showing PDF (and other document types) inline with the use of the no-cache header has been filed as a bug to Microsoft: http://support.microsoft.com/kb/316431. IE uses its own caching mechanism when reading PDFs inline.
Unfortunately, the folks at M$ said this "works as designed" and users should not use the no-cache header... go figure.
You could try VSU's idea of using a Java PDF reader... I may go this route too.
Controlling the cache settings down the pipe is not fool proof. The alternative is to encode the realtime and date in the file name of the PDF.
You can encode the time date into the filename of the PDF so that each time a request is made the filename is unique.
Response.AddHeader "Content-Disposition","attachment;filename=somename" + CurrentDate() + Currenttime() ".pdf"
CurrentDate adn CurrentTime are imaginary functions. You need to write that code.