Security News
Research
Data Theft Repackaged: A Case Study in Malicious Wrapper Packages on npm
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
The downloadjs npm package is a simple utility for triggering file downloads in the browser. It allows you to programmatically download files of various types, such as text, images, and binary data, directly from JavaScript.
Download Text Files
This feature allows you to download text content as a file. The code sample demonstrates how to create a text file named 'hello.txt' with the content 'Hello, world!' and trigger its download.
const download = require('downloadjs');
const text = 'Hello, world!';
download(text, 'hello.txt', 'text/plain');
Download Images
This feature allows you to download images. The code sample fetches an image from a URL, converts it to a Blob, and triggers its download as 'image.png'.
const download = require('downloadjs');
const imageUrl = 'https://example.com/image.png';
fetch(imageUrl)
.then(response => response.blob())
.then(blob => download(blob, 'image.png', 'image/png'));
Download Binary Data
This feature allows you to download binary data. The code sample demonstrates how to create a binary file named 'binary.bin' with some sample binary data and trigger its download.
const download = require('downloadjs');
const binaryData = new Uint8Array([0x00, 0x01, 0x02, 0x03]);
download(binaryData, 'binary.bin', 'application/octet-stream');
The file-saver package is a well-known library for saving files on the client-side. It provides similar functionality to downloadjs, allowing you to save text, images, and binary data. However, file-saver is more widely used and has better support for various file types and browsers.
The browser-filesaver package is another alternative for saving files in the browser. It offers similar capabilities to downloadjs, such as saving text and binary data. It is a lightweight library and can be a good choice for projects that need a simple solution for file downloads.
========
The download() function is used to trigger a file download from JavaScript.
It specifies the contents and name of a new file placed in the browser's download directory. The input can be a URL, String, Blob, or Typed Array of data, or via a dataURL representing the file's data as base64 or url-encoded string. No matter the input format, download() saves a file using the specified file name and mime information in the same manner as a server using a Content-Disposition HTTP header.
npm install downloadjs
bower install downloadjs
require("downloadjs")(data, strFileName, strMimeType);
download
function via <script>
includedownload(data, strFileName, strMimeType);
require(['path/to/file'], function(download) {
download(data, strFileName, strMimeType);
});
download("hello world", "dlText.txt", "text/plain");
download("data:text/plain,hello%20world", "dlDataUrlText.txt", "text/plain");
download(new Blob(["hello world"]), "dlTextBlob.txt", "text/plain");
download("/robots.txt");
var str= "hello world", arr= new Uint8Array(str.length);
str.split("").forEach(function(a,b){
arr[b]=a.charCodeAt();
});
download( arr, "textUInt8Array.txt", "text/plain" );
download(document.documentElement.outerHTML, "dlHTML.html", "text/html");
download(new Blob(["hello world".bold()]), "dlHtmlBlob.html", "text/html");
(note that callback mode won't work on vanilla ajax or with binary files)
$.ajax({
url: "/download.html",
success: download.bind(true, "text/html", "dlAjaxCallback.html")
});
download("/diff6.png");
var x=new XMLHttpRequest();
x.open( "GET", "/diff6.png" , true);
x.responseType="blob";
x.onload= function(e){download(e.target.response, "awesomesauce.png", "image/png");};
x.send();
download.js works with a wide range of devices and browsers.
You can expect it to work for the vast majority of your users, with some common-sense limits:
Can I tell when a download is done/canceled?
No.How can I style the temporary download link?
Define CSS class styles for .download-js-link
.What's up with Safari?
I don't know either but pull requests that improve the situation are welcome.Why is my binary file corrupted?
Likely: an incorrect MIME or using jQuery ajax, which has no bin support.How big of files work?
Depends, try yourself: File Echo Demo... I do a 1GB dl routinely on a thinkpad...FAQs
file downloading using client-side javascript
We found that downloadjs demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Research
The Socket Research Team breaks down a malicious wrapper package that uses obfuscation to harvest credentials and exfiltrate sensitive data.
Research
Security News
Attackers used a malicious npm package typosquatting a popular ESLint plugin to steal sensitive data, execute commands, and exploit developer systems.
Security News
The Ultralytics' PyPI Package was compromised four times in one weekend through GitHub Actions cache poisoning and failure to rotate previously compromised API tokens.