[Code] Directly ask the client browser to download the known type of file net_lover (original) Web developers all have such questions about how to send a file, especially a known type of file, to the client and directly prompt the viewer to download it, instead of opening it with the associated Program . 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 extension of the downloaded file, 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 file 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. We sometimes writeCodeIn order 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. In this way, the HTML generated by the ASP page can also beSource codeSave as a file, and the following code prompts you to save the result of ASP execution to the test.htm 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 '>" 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 !! [/Code] |