When downloading Word files, do not open them and download them as files. Are there any problems?

Source: Internet
Author: User
Tags file url
When downloading Word files, do not open them and download them as files. Are there any problems?

Because I have an 11-page Word file and click it to display the content, it is very slow. Have you downloaded it as a file? Could you please help me? Thank you!

Follow

Can you package it into a RAR file for download ~~

Data Flow

Method 1: Enter the name next to the connection (click "save" on the right and select "Save as target)

Method 2: Install IE6 SP1 or later

Method 3: If the server supports dynamic language, send the Content-Type File Header of application/octet-stream, and then send the file

I tried the upstairs method, but I still can't. Can I explain it in detail?
My system is ASP.

[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]

ASP is implemented in this way!

<%
'Download Function
'If filename is not empty, rename the file to filename for download.

Call downloadfile (Request ("file"), request ("FILENAME "))
Function downloadfile (strfile, filename)

'Get the path of the specified file
Strfilename = server. mappath (strfile)
'Response. Write (strfile)
'Response. End ()
'Clear Cache
Response. Buffer = true
Response. Clear

'Create a data stream
Set S = server. Createobject ("ADODB. Stream ")
S. Open
'Set as binary
S. type = 1
'Load in the file
On Error resume next

'Check whether the file exists
Set FSO = server. Createobject ("scripting. FileSystemObject ")
If not FSO. fileexists (strfilename) then
Response. Write ("<SCRIPT> alert ('the file you want to download does not exist! '); History. Back (); </SCRIPT> ")
Response. End
End if

'Get the object Length
Set F = FSO. GetFile (strfilename)
Intfilelength = f. Size

S. loadfromfile (strfilename)
If err then
Response. Write ("<SCRIPT> alert ('" & err. Description & "'); history. Back (); </SCRIPT> ")
Response. End
End if

'Redefines the browser window header file to download stream files.
If filename <> "" then
Response. addheader "content-disposition", "attachment; filename =" & filename
Else
Response. addheader "content-disposition", "attachment; filename =" & F. Name
End if
Response. addheader "Content-Length", intfilelength
Response. charset = "UTF-8"
Response. contenttype = "application/octet-stream"

'Implement the download function on the user end
Response. binarywrite S. Read
Response. Flush

'Final sorting
S. Close
Set S = nothing

End Function
%>

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.