JS implementation simple Download

Source: Internet
Author: User

For example, the address to download the file is: Htpp://127.0.0.1/test.rar


This method has no effect on Firefox, it is available on IE browser
window.open ("Htpp://127.0.0.1/test.rar");

This method is not supported by some versions of Firefox
Window.location.href= "Htpp://127.0.0.1/test.rar";

In order to solve some versions of Firefox does not support, can be changed to this way
Window.location= "Htpp://127.0.0.1/test.rar";

//This method can be both IE and Firefox,
//url represents the path of the file to be downloaded, such as: Htpp://127.0.0.1/test.rar
function downloadFile (URL) {  
   try{
        var elemif = document.createelement ("iframe");   
        elemif.src = url;  
         ElemIF.style.display = "None";  
        Document.body.appendChild (ELEMIF);  
   }catch (e) {
         zzrw.alert ("Download exception! ");
   }    
}


Form Direct Download file
The URL represents the file path to be downloaded, such as: Htpp://127.0.0.1/test.rar
function DownloadFile (URL) {
var form=$ ("<form>");//define form forms, send requests via form
Form.attr ("Style", "display:none");//set to not display
Form.attr ("Target", "");
Form.attr ("Method", "get");//Set Request type
Form.attr ("action", URL);//Set Request path
$ ("Body"). Append (form);//Add form to page (body)
Form.submit ();//form submission
}


Do not jump page request background download, background return a data flow, can be implemented through the form
As follows:

jquery's Ajax functions return type only XML, text, JSON, HTML and other types, there is no "stream" type, so we want to implement Ajax download, can not use the corresponding Ajax functions for file download. But you can use JS to generate a form, use this form to submit parameters, and return the "stream" type of data. The page is not refreshed during the implementation process.

Get request Download

The URL represents the request path, goes into background processing, and returns a file stream in the background
For example, the URL is htpp://127.0.0.1/test
function DownloadFile (URL) {

Define a form form to send requests via form form
var form=$ ("<form>");

Set form status to not display
Form.attr ("Style", "Display:none");

Method property setting request type is get
Form.attr ("Method", "get");

The Action property sets the request path, which can be followed directly by the path, if necessary.
Example: htpp://127.0.0.1/test?id=123
Form.attr ("action", URL);

Place the form in the page (body)
$ ("Body"). Append (form);

Form submission
Form.submit ();
}

Post request download


The URL represents the request path, goes into background processing, and returns a file stream in the background
For example, the URL is htpp://127.0.0.1/test
function DownloadFile (URL) {

Define a form form to send requests via form form
var form=$ ("<form>");

Set form status to not display
Form.attr ("Style", "Display:none");

Method property setting request type is post
Form.attr ("Method", "post");

The Action property sets the request path,
The path followed by the parameter is not available when the request type is post
Parameters can be passed through input in the form
Form.attr ("action", URL);
$ ("Body"). Append (form);//Place the form on the Web

Add input tags to a form to pass parameters
Multiple input tags can be added if multiple parameters are available
var input1=$ ("<input>");
Input1.attr ("type", "hidden");//Set as hidden field
Input1.attr ("name", "id");//Set parameter name
Input1.attr ("value", "123");//Set parameter value
Form.append (INPUT1);//Add to form

Form.submit ();//form submission
}

JS implementation simple Download

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.