Home > Web Front-end > JS Tutorial > body text

About the simple operation of downloading files in js (code attached, detailed answer)

亚连
Release: 2018-05-18 10:25:30
Original
2103 people have browsed it

The following is a simple operation for downloading files in js that I have compiled for you. Interested students can take a look.

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

//该方法在火狐上没有效果的,在IE浏览器上是可以的window.open("htpp://127.0.0.1/test.rar");
//该方法火狐有些版本是不支持的window.location.href="htpp://127.0.0.1/test.rar";
//为了解决火狐有些版本不支持,可以改成这种方式window.location="htpp://127.0.0.1/test.rar"; 
//该方法IE和火狐都可以,
//url表示要下载的文件路径,如:
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("下载异常!");
    }     
}
//表单方式直接下载文件
//url表示要下载的文件路径,如:
htpp://127.0.0.1/test.rar
function downloadFile(url)
{
    var form=$("<form>");//定义form表单,通过表单发送请求
    form.attr("style","display:none");//设置为不显示
    form.attr("target","");
    form.attr("method","get");//设置请求类型  
    form.attr("action",url);//设置请求路径
    $("body").append(form);//添加表单到页面(body)中
    form.submit();//表单提交
}1234567891011121314151617181920212223242526272829303132333435363738394041424344
Copy after login

Do not jump to the page to request background download , the background returns a data stream, which can be implemented through a form
As follows:

The return types of JQuery's ajax function are only xml, text, json, html and other types, and there is no "stream" type, so we have to implement Ajax download, you cannot use the corresponding ajax function to download files. But you can use js to generate a form, use this form to submit parameters, and return "stream" type data. During the implementation process, the page was not refreshed.

get request download

//url表示请求路径,进入后台处理,后台返回一个文件流//例如:url为htpp://127.0.0.1/test
function downloadFile(url){
    //定义一个form表单,通过form表单来发送请求
    var form=$("<form>");    //设置表单状态为不显示
    form.attr("style","display:none");    //method属性设置请求类型为get
    form.attr("method","get");    //action属性设置请求路径,(如有需要,可直接在路径后面跟参数)
    //例如:htpp://127.0.0.1/test?id=123
    form.attr("action",url);    //将表单放置在页面(body)中
    $("body").append(form);    //表单提交
    form.submit();
}12345678910111213141516171819202122232425262728
Copy after login

post request download

//url表示请求路径,进入后台处理,后台返回一个文件流
//例如:url为htpp://127.0.0.1/test
function downloadFile(url){
//定义一个form表单,通过form表单来发送请求
var form=$("<form>");
//设置表单状态为不显示
form.attr("style","display:none");
//method属性设置请求类型为post
form.attr("method","post");
//action属性设置请求路径,
//请求类型是post时,路径后面跟参数的方式不可用
//可以通过表单中的input来传递参数
form.attr("action",url);
$("body").append(form);//将表单放置在web中
//在表单中添加input标签来传递参数
//如有多个参数可添加多个input标签
var input1=$("<input>");
input1.attr("type","hidden");//设置为隐藏域
input1.attr("name","id");//设置参数名称
input1.attr("value","123");//设置参数值
form.append(input1);//添加到表单中
form.submit();//表单提交
}
Copy after login

The above is a simple operation for downloading files in js that I compiled for you. I hope it will be helpful to everyone in the future.

Related articles:

Problems with adding ! in front of function in js, the code is attached

Explain JS in detail How to interact with the app (code attached)

Detailed explanation of the use of Js apply() (including code)

The above is the detailed content of About the simple operation of downloading files in js (code attached, detailed answer). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!