Download file from URL, then save to user computer via dialog - javascript

How can I download file (I just know the URL - the file can have x MB) and then save via dialog to user computer? (Dialog = user can choose the directory, where he want to save the file). Just like in screen.
I have absolutely no idea how to do it. I Google it and I found only how to load the file via FILE input, but I do not need that.
File suffix is "*.db", thanks guys, I'm not such a web guy.

You can do that pretty easily.
Make a php file, let's call it index.php. And this should be all the code you need:
<?php
$file = "/your/location/file.db";
header("Content-Type: application/octet-stream");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename='" . basename($file) . "'");
readfile ($file);
?>
I hope I've understood what you actually need. This should do the trick.

Related

Read pdf without download and print it with html php

Good evening,
I would like to create a feature for my users that allows them to read a pdf file without downloading or printing it.
They will only be able to read the pdf file.
I tried pdf.js which did not work,
I also tried to convert my pdf to html via a script,
I also tried this php code but the download and print button appears:
<?php
// The location of the PDF file
// on the server
$filename = "/path/to/the/file.pdf";
// Header content type
header("Content-type: application/pdf");
header("Content-Length: " . filesize($filename));
// Send the file to the browser.
readfile($filename);
?>
Do you have any solutions or technologies to suggest to me,
I use PHP, HTML and JS.
Try this one:
<?php
// Store the file name into variable
$file = '/path/to/the/filename.pdf';
$filename = 'filename.pdf';
// Header content type
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $filename . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
// Read the file
#readfile($file);
?>
PHP uses a standard code to display the pdf file in web browser. The process of displaying pdf involves location of the PDF file on the server and it uses various types of headers to define content composition in form of type, Disposition, Transfer-Encoding etc. PHP passes the PDF files to read it on the browser. Browser either shows it or download it from localhost server then display pdf.
Note: PHP is not actually reading the PDF file. It does not recognize File as pdf. It only passes the PDF file to the browser to be read there. If copy the pdf file inside htdocs folder of XAMPP then it does not need to specify the file path.
The php script you offered only can download the file. if you use php ,you can try the demo ,which is using pdf2text here
or try the method in this link read-pdf-files-with-php

Force IE11 to save download CSV file

I am trying to download a CSV file to the browser.
I am unable to get it to properly download the file in IE11. Instead it opens a new tab and write the CSV contents to the tab.
I am using just the following javascript:
window.open(sUrl, '_target');
In the IE Developer tools, I can see the Response Header shows:
Content-Type: text/csv; charset=UTF-8
I would like it to prompt the user to save the file.
I've changed the browser settings to always download, yet it still doesn't do it.
I would like to fix this from a change to the Javascript, if possible.
How can I do that?
As far as I know, it's not possible to do this with JavaScript. Take a look at this answer if you're comforable using PHP to download the file.
According to that answer, your PHP should look something like this:
<?php
$file_url = 'http://www.myremoteserver.com/file.csv';
header('Content-Type: text/csv; charset=UTF-8');
header("Content-Transfer-Encoding: Quoted-Printable");
header("Content-disposition: attachment; filename=\"" . basename($file_url) . "\"");
readfile($file_url);
?>

C JAVA PHP TXT ... read instead download

I have website, and I already can highlight code that is written in HTML.
I can store documents on my website, but when I store document (like .php , .c, , .cpp) I can only download them, not opening in next window.
I need some idea how to pass raw code of that file to another page.
I have apache server and I use PHP
I'm not 100% sure what you're trying, but how about posting the file name to a PHP script and reading the file contents using fopen/fread and then just echo it? You should of course make sure that it'll only output files in a specific folder, so this can't be used to hack your site!
<?php
if(isset($_POST['filename']) && is_allowed_to_be_shown($_POST['filename']))
{
$filename = $_POST['filename'];
$handle = fopen($filename, "r");
$contents = fread($handle, filesize($filename));
fclose($handle);
echo $contents;
}
?>
Try accessing the pages ending in .php, .c, .cpp, etc via typing in "localhost/[path to file]" in the address bar. That way the files are read by the sever, before being outputted to the browser, meaning that .php (and the like) files will be handled correctly rather than just HTML files.

How do i rename a file on download using HTML5?

i have been working on a website which will allow users to upload and download files. The file is renamed on upload and stored on my server. But when it is downloaded, it has to be renamed i.e to its original name. Presently i am using the following to do so :
Download
But the "download" attribute does not work with firefox. Any alternative using javascript or jquery ? I am using php for server side.
Edit:
Thanks for the solution using php. But that is not what i am looking for. I am using a custom file viewer using javascript. On clicking the thumbnail of the file, the viewer is displayed. An AJAX request is sent to get the link to the file. The response (the link) is used to display the file, also is added to the tag for download. A php solution would mean a page reload on every request, which i want to avoid. So any javascript or jquery solutions ?
On the server use header("Content-Disposition: attachment; filename='Original name'")
Try Below Code
header('Content-Disposition: attachment; filename='.basename('newfilename.txt'));
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize('filename.txt'));
ob_clean();
flush();
readfile('filename.txt');

window.open does not open in IE

I am trying to open a word document using window.open as below
window.open("myworddoc.doc");
It works fine in FF, but IE tries to open a tab, but closes it immediately and jumps back to the current screen (no dialog is displayed to save or open a file).
What could be the issue?
This is surely a security mesure. Opening Word documents using JavaScript could have nasty effects. Imagine if you are browsing the internet, and someone makes an infected Word document open when your page loads.
Personally, I'd create a PHP file, let's say "servedoc.php", and open that file like so:
window.open("servedoc.php");
servedoc.php could contain something like this:
<?php
$file = "myworddoc.doc";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/msword");
header("Content-Transfer-Encoding: binary");
readfile($file);
IE will open the PHP file, as it's a perfectly valid web file. And the PHP script would serve the file to the browser, asking the user to download the file.
Or if you are using .net (vb):
Response.ContentType = "image/jpeg" 'mime type of the file to serve.
Response.AddHeader("content-Disposition", "attachment;filename=YOURFILENAME")
Response.TransmitFile(YourFILEPath)
Like this you can let them download the .doc or the .zip file if you prefer.

Categories

Resources