How can I download txt, Doc, xls, and other text files directly?

Source: Internet
Author: User
Tags file url
When I write a download link, how can I directly download txt, Doc, xls, and other text files without opening it? sunzhaoqing2001_1 (zhubei) 08:31:39 Web Development/ HTML (CSS)Q: Thank you! Question count: 0. replies: 3

Lions911 (-- Administrator --) on the first floor replied to-10-28 09:32:08 score 0 and asked the client browser to directly download known types of files [Post]

Date: 2002-3-23 22:34:03
Source: csdn
Author: Unknown

Web developers have such questions: how to send a file, especially a known type of file, to the client and directly prompt the viewer to download, instead of opening it with a program associated with it. In the past, the most common method was to add such a file to the link, so that the viewer can download the linked file by right-clicking the target and saving it as a download. However, there are two shortcomings:
First, if the browser can identify the downloaded file extension, the browser will activate the program associated with the extension to open the downloaded file. For example, on Windows platform, if the user clicks a chain connected to a .doc file, the browser starts the Microsoft Word application to open it.
Second, if you use a link, anyone who can see the link can download the file. Although you can also set permissions for the downloaded file, but that is not very convenient. Sometimes we need a more flexible and flexible way. The following procedures can easily overcome the above two shortcomings.
This method is reliable, but you must remember that unauthorized users cannot download the file by entering the File URL in the browser address bar. Therefore, the files to be downloaded should be placed in a directory other than the virtual directory. For example, if your virtual directory is C:/mengxianhui/atat4/website/MyApp, all files stored in this directory and any sub-directories under this directory are visible to any users on the Internet. To download an object directly, we need to do two things. The first thing is to set the response content class to "application/octet-stream", which is case-insensitive. The second thing is: Set the HTTP response header name to content-Disposition and set the value to attachment and filename = thefilename. Thefilename is the default file name that appears in the File Download Dialog Box. It is usually the same as the downloaded file name, but it can also be different. Below, we will give an example of the actual application of JSP and ASP pages that are commonly used.

Example of the testfiledownload. jsp page:

<%
// Obtain the file name and path.
String filename = "mengxianhuidoctest.doc ";
String filepath = "D ://";

// Set the response header and download and save the file name
Response. setcontenttype ("application/OCTET-STREAM ");
Response. setheader ("content-disposition ",
"Attachment; filename =/" "+ filename + "/"");

// Open the stream information of the specified file
Java. Io. fileinputstream =
New java. Io. fileinputstream (filepath + filename );

// Write the stream information
Int I;
While (I = fileinputstream. Read ())! =-1 ){
Out. Write (I );
}
Fileinputstream. Close ();
Out. Close ();
%>

It is worth noting that in the content of the file to be downloaded, except the content of the file, no other characters should be appended, including spaces and carriage return line breaks. Sometimes, when writing code, to make the code clear and readable, some spaces, tabs, or carriage return line breaks are often added. Although this looks clear, sometimes the correct results may not be obtained. For example:
<% @ Page import = "Java. Io .*"
%> <JSP: usebean id = "mybeanfrommengxianhui" Scope = "page"
Class = "com. mengxianhui. downloadbean"/>
It should be written as follows:
<% @ Page import = "Java. Io .*"
%> <JSP: usebean id = "mybeanfrommengxianhui" Scope = "page"
Class = "com. mengxianhui. downloadbean"/>

Example of the testfiledownload. ASP page:

In ASP, there is no way to read file stream information from a file. Therefore, to obtain file stream information, we must use other tools, the simplest thing is to compile a DLL component of VB or C so that the component can return the stream information of the file. The following is an example of a DLL written in VB. The project name is mengxhfiledownload and the class module name is binreadfromfile. The class method readbinfromfile is as follows:

Function readbinfromfile (byval bfilename as string) as Variant
Dim FL as long
Dim filenum as long
Dim binbyte () as byte
Dim binfilestr as string

On Error goto errhandler
Filenum = freefile
Open bfilename for binary as # filenum
FL = filelen (bfilename)
Redim binbyte (FL)
Get # filenum, binbyte
Close # filenum
Readbinfromfile = binbyte
Exit Function

Errhandler:
Exit Function
End Function

Compile the above Code into mengxhfiledownload. dll and register it. Here is an example of downloading a when a man loves a woman.mp3 MP3 file. The ASP script code we want to compile is as follows:

<% @ Language = VBScript %>
<%
Response. Buffer = true
Response. contenttype = "application/OCTET-STREAM"
Response. addheader "content-disposition", "attachment; filename = when a man loves a womanment"

Dim varstream, omyobject

Set omyobject = server. Createobject ("mengxhfiledownload. binreadfromfile ")
Varstream = omyobject. readbinfromfile ("E:/mengxianhui/MP3/when a man loves a womanves ")
Response. binarywrite (varstream)
Set omyobject = nothing

Response. End
%>

When we run the above testfiledownload. asp file, the browser will pop up a file download dialog box, prompting us to download, instead of opening it with the default MP3 player.
Hosts file. The specific method is:
<%
Response. contenttype = "application/OCTET-STREAM"
Response. addheader "content-disposition", "attachment?filename=test.htm"
Response. Write "<Div style = 'background-color: Navy; color: # ffff'> test </div>"
Response. Write "<a href = 'HTTP: // lucky.myrice.com & #39;>"
Response. Write " [wonderful world of the Meng xianhui] </a>"
Response. End
%>

When the number of files is small, you can also directly set them on the server side for direct download of these files. The specific method is: in the Internet Service Manager, select the "attribute" option, and then select the "HTTP headers" tab to set it !!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.