|
 | james on September 4, 2003 | Download to desktop
I am creating a personal web site and want to offer vistors the chance to download some of the music I have written. Currently I have a simple anchor tag that points to a mp3 object containing the sample. When clicked, the music is downloaded and starts playing (using whichever music software is installed). However, I want the visitor to have the chance to save the file to disk, which is not happening.
How do I get a download dialogue window to appear, prompting the user to either save the file to disk or to open it directly? |
|
 | Eric Georgieff on September 5, 2003 | RE: Download to desktop
James,
Is it possible for you to use some sort of server-side scripting language such as PHP, ASP or Perl?
If so I have a solution for you. A server side script that forces a file to download rather than be viewed by a default helper application.
Let me know,
Eric Georgieff |
|
| james on September 5, 2003 | RE: Download to desktop
Eric - thanks for the reply.
Yes I think I have both Perl and PHP possibilities (not too sure which, so would be interested in the answers to both of them!) ASP technology is out as the servers are not running Microsoft based packages.
Also, I have dabbled in both Perl and PHP, so I may even understand a little about your solution.
Cheers,
James |
|
| Eric Georgieff on September 5, 2003 | RE: Download to desktop
James,
If you are using PHP, the following little script should force the file to be downloaded rather than being viewed by the default helper application.
<?
$path = "c:/archive/frame1.bmp";
$file = basename($path);
$size = filesize($path);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $size");
readfile($path);
?>
Just set the value of the $path variable to the actual location of the file you want to have downloaded by the user. As well, this should appear at the top of a *.php file, before any output has been sent to the browser, since the headers must be sent first.
If you would prefer to have a single universal script that allows you to easily force download any file available on your site you can try the following:
<?
$path = $HTTP_GET_VARS['filepath'];
$file = basename($path);
$size = filesize($path);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $size");
readfile($path);
?>
Then you can simply modify the value of the $path variable on a case by case basis, by linking to the file containing this code and sending the appropriate GET parameter.
For instance, if you save that code in a file called "forcedownload.php", then to force download the file at "c:/archive/frame1.bmp" you could simply make a like pointing to "forcedownload.php?filepath=c:/archive/frame1.bmp"
In HTML it would be something like:
<a href="forcedownload.php?filepath=c:/archive/frame1.bmp">Download frame1.bmp</a>
Of course, if you don't want to give away the file structure of your server, you could just place the standard path (like c:/archive in this example) in the php source code itself and merely append the file name to that path. This would probably be done using a script similar to the following:
<?
$path = "c:/archive/".$HTTP_GET_VARS['filename'];
$file = basename($path);
$size = filesize($path);
header ("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: $size");
readfile($path);
?>
And then you could just link to your force file downloader without so much information in the GET variable.
The file link for our example would become:
forcedownload.php?filename=frame1.bmp
This last script is probably the most desireable for your situation.
I hope this helps,
Eric Georgieff |
|
| james on September 5, 2003 | RE: Download to desktop
Wow - Eric, that is brilliant, and in addition... even I can get it to work. Thanks for all the help...
Cheers,
James |
|
| mauve928 on July 28, 2004 | RE: Download to desktop
hi!
wanna say thanks to eric for the post, and james for asking the question.. thank you both! :) i have been looking for this snippet since the other day.. all i found were program/tempalte (open source), but i dont want to use those program (since i cant understand them).. :)
thank you again.. :) |
|
| krang on August 17, 2004 | RE: Download to desktop
Eric Georgieff, dont use...
$path = "c:/archive/".$HTTP_GET_VARS['filename'];
what happens if someone tampers with the get request, eg...
index.php?filename=../../windows/system32/file
would retrive the file
c:/windows/system32/file
you could use...
$path = $HTTP_GET_VARS['filename'];
$path = str_replace('/', '', $path);
$path = str_replace('\\', '', $path);
$path = "c:/archive/".$path;
which will remove any slashes file the filename, so only files within that folder could be accessed.
Also, as a final note - use a *nix system, there much better at... well everything :-) |
|
| afiz on September 14, 2004 | RE: Download to desktop
can this be done to word document to |
|
| afiz on September 14, 2004 | RE: Download to desktop
I'm new to php and working on a system.It requires to create a report on words document and save it to the client PC.The creating words document part have being solve but the problem is that the document is stored on the server.Is there any way that the document can be save to the client PC. |
|
| Rishi on March 28, 2005 | RE: Download to desktop
How do I get a download dialogue window to appear, prompting the user to either save the file to disk or to open it directly in ASP?
I'm using .Net IDE
At present I used some sort of code and it is workig but downloading dialog comes twice and if i open the file, it is reading from the DataBase and opene it properly. If I clicked again on the view link, then another window opening and down load dialogue appears in new window. I want to prevent it and can easily do again and again .... See used code below:
private void WriteFileToClient(int attachmentId)
{
string query = string.Format("AttachmentId = {0}", attachmentId);
DataRow[] dataRows = attachmentDataset.Attachment.Select(query);
if (dataRows.Length > 0)
{
AttachmentDataset.AttachmentRow mailAttachmentsRow = (AttachmentDataset.AttachmentRow )dataRows[0];
int contentLength = mailAttachmentsRow.ContentLength;
string fileName = mailAttachmentsRow.FileName;
string contentType = mailAttachmentsRow.ContentType;
byte[] Data = mailAttachmentsRow.FileData;
Response.Clear();
Response.ContentType = contentType;
Response.AppendHeader("Content-Disposition", "attachment;filename=" + fileName);
Response.OutputStream.Write(Data, 0, contentLength);
Response.End();
Response.Flush();
}
}
expecting somebody's help |
|
| RiCkY on May 18, 2005 | RE: Download to desktop
rishi: add this somewhere ;)
Response.AppendHeader("Content-disposition","attachment; filename="+filename); |
|
| robbie on November 10, 2005 | RE: Download to desktop
if u wanna play ot test server this is ur websitehttp://dragonzden.no-ip.biz/
u gotta download it. its a bit tricky but if i can get it to work so can you if u think its good connect me in there. with my char name . Sir Robert The Druid and u mess with me u die cya hope u enjoy the website urs truly robbie |
|
| grantmx on August 24, 2007 | RE: Download to desktop
I know this is way old, but its sweet an still works. For the non PHP folks like me, make sure you include the FULL path from the root of your server (even if hosted remotely) in the $path ="".$path; line.
So an absolute URL ("http://...") won't work, nor will "/yourfiledirectory/", however something like "/home/httpdocs/www/yourfiledirectory/" (or what ever is the path or your hosting server) will.
It took me a while to figure that out. Duh! |
|
| aaabbbccc on August 4, 2011 | RE: Download to desktop
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497
]//siliconguide/captcha/securimage_show.php?0.566346006969497 |
|
| Mahesh on September 20, 2011 | RE: Download to desktop
When file name is multibyte(japanese), file name gets distored while
downloading |
|
| S N JHA on April 2, 2012 | RE: Download to desktop
Hope it works, Thanks. |
|
| Provide an Answer |
Didn't find what you were looking for on this page?
Browse all Programming related questions
Browse all Questions and Answers
|
|
|