PHP - How to create download link - Asked By Olusegun A. on 07-Aug-10 01:24 PM

Hello friends,

please how do i create a download link through which once my visitor click on the product or the download link they will be able to download files like MSWord format, PDF format, Audios and Videos.

Regards,

adsegzy 

Super Man replied to Olusegun A. on 07-Aug-10 01:29 PM
first you have to make the item in format like MSWord format, PDF format, Audios and Videos..


then an only you can provide links download in any format.


or if you have any online conversion tool , which enables you to convert one form to another form, then you can generate runtime item and you can provide link to user, which must be temporary link.
Ken Fitzpatrick replied to Olusegun A. on 07-Aug-10 03:17 PM
Olusegun,

Here is an example of how you can do this using VB.Net. You can easily do it in C#, but the syntax is obviously slightly different, but the concept is the same. In the following example, I have a PDF file called AndroidDevelopment.pdf I want to provide it as a download (replace with your own pdf filename and path). Here is how it is done:
1. Create an aspx page called Download.aspx.
2. Add a LinkButton to the page:
<asp:LinkButton ID="pdfLink" runat="server">Android Development</asp:LinkButton>
Since what we are going to trap is the OnClick event, you don't have to use a LinkButton, you can use anything you want that produces Onclick events or any event for that matter. You just need to trigger the code in step three to initiate the transfer.
3. In the code-behind, add a handler for the OnClick and add the following code to the handler:
Protected Sub pdfLink_Click(ByVal sender As Object, ByVal e As EventArgs) Handles pdfLink.Click
   Response.ContentType = "application/pdf"
   Response.AppendHeader("Content-Disposition", "attachment; filename=AndroidDevelopment.pdf")
   Response.TransmitFile(Server.MapPath("~/pdf/AndroidDevelopment.pdf"))
   Response.End()
End Sub
4. Open Download.aspx in the browser and see how it looks. It should be what you are looking for.

Here is how it works:
1. when the click event fires, The first line, Response.ContentType set the Mime type of data that will be set back to the browser. In this case, it is "application/pdf". If you are sending jpg files, then it will be "image/jpg". For Word files it will be "application/ms-word". To figure out the proper Mime type, do a search using Google. I did the following search to find the pdf content type: response.contenttype pdf files.
2. The next line, Response.AppendHeader adds some more information to tell the browser you have an attachment. Just make sure the filename parameter is the name of the file. This is what shows up in the Save Dialog box.
3. The third line tells the server what file it needs to send to the browser.
4. The final line ends the Response.

As long as you provide some sort of clickable link and the Response code on the server for the correct file, it should work fine for you. Let me know how it goes.

Ken