10 examples of 'reactjs download file from api' in JavaScript

Every line of 'reactjs download file from api' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
21function download({ subtitle }: { subtitle: SubtitleModel }): Promise<DownloadReturn> {
22 return axios
23 .get(subtitle.link, { responseType: 'stream' })
24 .then(extractFile)
25 .then(() => ({
26 subtitle,
27 outDir: rootPath,
28 }));
29}
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

86export function download(url = "", showLoading = true) {
87 if (showLoading) wx.showLoading({title: '加载中',});
88 return new Promise((resolve, reject) => {
89 wx.downloadFile({
90 url: url,
91 success(res) {
92 resolve(res.tempFilePath);
93 },
94 fail(res){
95 reject(res);
96 },
97 complete(){
98 if (showLoading) wx.hideLoading();
99 }
100 })
101
102 });
103}
16function download(url: string, filename: string) {
17 console.log(`download ${url}...`)
18 return new Promise<void>((resolve) => {
19 request(url, resolve).pipe(fs.createWriteStream(filename))
20 });
21}
20function download(name) {
21 const url = downloadDescription[name];
22 const targetFile = getTargetFileName(name);
23
24 return new Promise((resolve, reject) => {
25 console.log(chalk.cyan(`Downloading syntax ${name} ...`));
26 console.log(chalk.cyan.dim(` ${url}`));
27
28 request(url, {}, (err, response, body) => {
29 if (err) {
30 console.error(chalk.red(`Download failed! ${err.message}`));
31 console.error(chalk.red(err.stack));
32 return reject(err);
33 }
34 if (response.statusCode != 200) {
35 console.error(chalk.red(`Download failed! because the response code is not 200`));
36 console.error(chalk.red(` actual: ${response.statusCode} ${response.statusMessage}`));
37 return reject(new Error(`response code is not 200, but ${response.statusCode}`));
38 }
39
40 if (body instanceof Buffer)
41 body = body.toString('utf8');
42 console.log(chalk.green(`Download success! (length: ${body.length})`));
43
44 writeFile(targetFile, name, body);
45 console.log(chalk.green(`Written to "${targetFile}" success!`));
46
47 if (name == defaultSyntax) {
48 writeFile(defaultFileName, name, body);
49 console.log(chalk.green(`Written to "${defaultFileName}" success!`));
50 }
51 return resolve(body);
52 });
53 });
54}
477onDownloadFile (filename, href) {
478
479 let a = document.createElement('a')
480
481 a.download = filename
482 a.href = href
483
484 a.click()
485}
62*/},{key:"download",value:function download(){if(this.instance.pdfExists){this.instance.download()}}}],[{key:"template",get:function get(){return(0,_polymerElement.html)(_templateObject_a9789b60ab1411e99f2671cfdff57fb5())}},{key:"tag",get:function get(){return"pdf-element"}},{key:"properties",get:function get(){var props={/**
166download() {
167 const files = osFilterObj(this.src() || []);
168 const urls = [];
169
170 if (files.length === 0) {
171 return Promise.reject(new Error('No binary found matching your system. It\'s probably not supported.'));
172 }
173
174 files.forEach(file => urls.push(file.url));
175
176 return Promise.all(urls.map(url => download(url, this.dest(), {
177 extract: true,
178 strip: this.options.strip
179 }))).then(result => {
180 const resultingFiles = flatten(result.map((item, index) => {
181 if (Array.isArray(item)) {
182 return item.map(file => file.path);
183 }
184
185 const parsedUrl = url.parse(files[index].url);
186 const parsedPath = path.parse(parsedUrl.pathname);
187
188 return parsedPath.base;
189 }));
190
191 return Promise.all(resultingFiles.map(fileName => {
192 return chmodAsync(path.join(this.dest(), fileName), 0o755);
193 }));
194 });
195}
34mount() {
35 const el = this.el();
36 el.className = "vjs-control vjs-button vjs-customer-button vjs-download";
37 ReactDOM.render(<DownLoad vjsComponent={this} />, el);
38}
5function loadJs({
6 url,
7 options = {}
8}) {
9 return new Promise((resolve, reject) => {
10 if (!url) {
11 reject(new Error('NO URL'))
12 }
13 const timeout = options.timeout || 60000
14 const target = document.getElementsByTagName('head')[0] || document.head
15 let timer; let
16 script
17 // 超时自动取消
18 if (timeout) {
19 timer = setTimeout(() => {
20 // eslint-disable-next-line no-use-before-define
21 cleanup()
22 reject(new Error('Timeout'))
23 }, timeout)
24 }
25 // 清理函数
26 function cleanup() {
27 if (script.parentNode) {
28 script.parentNode.removeChild(script)
29 }
30 if (timer) {
31 clearTimeout(timer)
32 }
33 }
34 // 创建js文件
35 script = document.createElement('script')
36 script.type = 'text/javascript'
37 script.charset = 'utf-8'
38 script.async = true
39 script.src = url
40 target.appendChild(script)
41 // IE的script 元素支持onreadystatechange事件,不支持onload事件 FF的script
42 // 元素不支持onreadystatechange事件,只支持onload事件
43 // eslint-disable-next-line no-multi-assign
44 script.onload = script.onreadystatechange = function () {
45 if (!script.readyState || script.readyState === 'loaded' || script.readyState === 'complete') {
46 resolve()
47 if (timer) {
48 clearTimeout(timer)
49 }
50 }
51 }
52 })
53}
13downloadRandomFile() {
14 return fetch('http://localhost:8080/resources/files');
15}

Related snippets