js| Control | Download from Mengxian will be big blog
Web developers have the question of how to send a file, especially a file of a known type, to the client, prompting the viewer to download it, rather than opening it with the program associated with it. Previously our most common method is to add such a file to the link, so that viewers can click the right mouse button to save the target to download the linked files. However, there are two deficiencies in this area:
First: If the browser can recognize the extension of the downloaded file, the browser activates the program associated with the extension to open the downloaded file. For example, on a Windows platform, if a user clicks on a link that links to a ". doc" file, the browser launches the Microsoft Word application to open it.
The second is: if you use the link, any person can see the link can download the file, although you can also download the file permission settings, but it is not very convenient. There are times when we need a more flexible and resilient approach, and the following procedures can easily overcome both of these deficiencies.
This approach is reliable, but you must keep in mind that no authorized user can obtain the download rights of the file by entering the URL of the file in the browser's address bar. Therefore, the file to be downloaded should be placed in a directory other than the virtual directory, such as: If your virtual directory is C:\Mengxianhui\Tomcat4\Website\MyApp, then, All files stored in the directory and any subdirectories under that directory are visible to any user on the Internet. To download a file directly, we need to do two things, the first thing is: Set the response of the content class is "Application/octet-stream", case-insensitive. The second thing is: Set HTTP response header name: Content-disposition, set value is: attachment; filename = thefilename. The thefilename here is the default file name that appears in the File Download dialog box, usually the same name as the file being downloaded, but it can also be different. Below, we use the most commonly used JSP and ASP page to give a practical example.
Examples of testfiledownload.jsp pages:
<%
Get file name and path
String filename = "Mengxianhuidoctest.doc";
String filepath = "d:\\";
Set the response header and download the saved file name
Response.setcontenttype ("Application/octet-stream");
Response.setheader ("Content-disposition",
"Attachment; Filename=\ "" + filename + "\");
Opens flow information for the specified file
Java.io.FileInputStream FileInputStream =
New Java.io.FileInputStream (filepath + filename);
Write out flow 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 you want to download, in addition to the contents of the file, there should be no additional characters, including spaces and carriage return line feed. We sometimes write code, in order to make the code clear and readable, often add some space, tab or carriage return line breaks, so that although it looks more clear, but sometimes may not get the correct results. Like what:
<%@ page import= "java.io.*"
%> <jsp:usebean id= "Mybeanfrommengxianhui" scope= "page"
Class= "com. Mengxianhui.downloadbean "/>
It should be written like this:
<%@ page import= "java.io.*"
%><jsp:usebean id= "Mybeanfrommengxianhui" scope= "page"
Class= "com. Mengxianhui.downloadbean "/>
Examples of testfiledownload.asp pages:
In ASP, there is no way to read the file flow information from the file, therefore, in order to get the file flow information, we must use other tools, the simplest is to write a VB or C DLL components, so that the component returns the file flow information. The following is an example of a DLL written in VB, the project name is Mengxhfiledownload, class module name is Binreadfromfile, class method Readbinfromfile 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 then register to use it. The following is an example of a MP3 file that downloads a Man loves a Woman.mp3 directly, and the ASP script code we want to write is as follows:
<%@ Language=vbscript%>
<%
Response.Buffer = TRUE
Response.ContentType = "Application/octet-stream"
Response.AddHeader "Content-disposition", "Attachment;filename=when a Man Loves a Woman.mp3"
Dim Varstream, Omyobject
Set omyobject = Server.CreateObject ("Mengxhfiledownload.binreadfromfile")
Varstream = Omyobject.readbinfromfile ("E:\MengXianhui\Mp3\When A Man Loves a Woman.mp3")
Response.BinaryWrite (Varstream)
Set Omyobject = Nothing
Response.End
%>
When we run the testfiledownload.asp file above, the browser pops up a file Download dialog box that prompts us to download it instead of using the default MP3 player to open it.
This method can also save the HTML source code generated by our ASP page into a file, the following code prompts you to save the results of ASP execution into test.htm files. The specific methods are:
<%
Response.ContentType = "Application/octet-stream"
Response.AddHeader "Content-disposition", "attachment;filename=test.htm"
Response.Write "<div style= ' background-color:navy;color: #FFFFFF ' > Test </div>"
Response.Write "<a href= ' http://lucky.myrice.com ' >"
Response.Write "Response.End
%>
When we have a small number of files, can also be set directly on the server side, so that these files directly downloaded. In Internet Services Manager, select the "Properties" item and select the "HTTP Headers" tab to set it up!!
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.