Repository
Current version released
4 years ago
download ⤵️
import { download } from "https://deno.land/x/download/mod.ts";
Usage
SAMPLE 1 :
import { download } from "https://deno.land/x/download/mod.ts";
const url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
try {
const fileObj = await download(url);
} catch (err) {
console.log(err)
}
Alternatively :
import { download } from "https://deno.land/x/download/mod.ts";
const url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
download(url)
.then(fileObj => {
console.log(fileObj)
})
.catch(err => {
console.log(err)
});
By default, the module creates a temporary directory every time you call the download
function and downloads the file into it.
You can specify the download destination, filename and also the file permission via the second parameter.
Note :As of now download directory should be present, else error will be thrown. This will not be the case in future.
// def of 2nd parameter. ./type.ts
Destination {
dir?: string,
file?: string,
mode?: number
}
SAMPLE 2 :
import { download, Destination } from "https://deno.land/x/download/mod.ts";
const url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
try {
// NOTE : You need to ensure that the directory you pass exists.
const destination: Destination = {
file: 'example.pdf',
dir: './test'
}
/* sample with mode
const destination: Destination = {
file: 'example.pdf',
dir: './test',
mode: 0o777
}
*/
const fileObj = await download(url, destination);
} catch (err) {
console.log(err)
}
Passing http methods and headers:
Behind the scene this module uses deno’s fetch api. The third parameter to download
function is RequestInit. You can pass body
, headers
, cache
, method
… the same way you pass to the fetch api.
SAMPLE 3 :
import { download, Destination } from "https://deno.land/x/download/mod.ts";
const url = 'https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf';
try {
const destination: Destination = {}
const reqInit: RequestInit = {
method: 'GET'
}
/* sample with mode
const destination: Destination = {
file: 'example.pdf',
dir: './test',
mode: 0o777
}
*/
const fileObj = await download(url, destination, reqInit);
} catch (err) {
console.log(err)
}
Return Value
download
function returns file
(filename), dir
, fullPath
, and size
(in bytes)
// definiton of return object. check:./type.ts
DownlodedFile {
file: string,
dir:string,
fullPath: string,
size: number
}
Deno Permission
You need --allow-net
and --allow-write
permission to use download
function.