Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

easy-file-picker

Package Overview
Dependencies
Maintainers
1
Versions
6
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

easy-file-picker

Easy File Picker is a straightforward library with no dependencies to upload/pick/read files in the browser.

  • 1.1.0
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
61
decreased by-31.46%
Maintainers
1
Weekly downloads
 
Created
Source

Easy File Picker

Easy File Picker is a straightforward library with no dependencies to upload/pick/read files in the browser that can be used with any frontend framework.

Table of Contents

Install

npm install easy-file-picker

Usage

Example on how to upload a file in various javascript frameworks:

Vanila Javascript

HTML:

<button id="uploader">Upload!</button>

Javascript/TypeScript:

import { getFile, uploadFilesTo } from 'easy-file-picker';

document.querySelector("#uploader").addEventListener("click", () => getFile().then((file) => { if(file) uploadFilesTo("http://example.com", file)}));

Angular

HTML:

<button (click)="uploadFile()">Upload!</button>

TypeScript:

import { getFile, uploadFilesTo } from 'easy-file-picker';

async uploadFile(): Promise<void> {
  const file = await getFile();
  if(file) {
    await uploadFilesTo("http://example.com", file);
  }
}

React

Javascript:

import { getFile, uploadFilesTo } from 'easy-file-picker';

async uploadFile() {
  const file = await getFile();
  if(file) {
    await uploadFilesTo("http://example.com", file);
  }
}

render() {
  return <button onClick={uploadFile}>Upload!</button>;
}

Vue

HTML:

<button @click="uploadFile">Upload!</button>

Javascript:

import { getFile, uploadFilesTo } from 'easy-file-picker';

methods: {
  async uploadFile() {
    const file = await getFile();
    if(file) {
      await uploadFilesTo("http://example.com", file);
    }  
  }
}

Svelte

Svelte:

<script>
import { getFile, uploadFilesTo } from 'easy-file-picker';

async function uploadFile() {
  const file = await getFile();
  if(file) {
    await uploadFilesTo("http://example.com", file);
  } 
}
</script>

<button on:click={uploadFile}>Upload!</button>

Functions

GetFile

Shows a system file dialog where the user can select a single file and returns it. Returns null if no file is selected.

function getFile(options?: FilePickerOptions): Promise<File | null>

GetFiles

Shows a system file dialog where the user can select multiple files and returns them. Returns empty array if no file is selected.

function getFiles(options?: FilePickerOptions): Promise<File[]>

GetFileAsString

Shows a system file dialog where the user can select a single file and returns a string representation of the file content. Returns null if no file is selected.

function getFileAsString(options?: FilePickerOptions): Promise<FileStringResult | null>

GetFilesAsString

Shows a system file dialog where the user can select multple files and returns string representations of the selected files content. Returns empty array if no file is selected.

function getFilesAsString(options?: FilePickerOptions): Promise<FileStringResult[]>

UploadFilesTo

Makes a HTTP request to the indicated url with the files as the body (content-type: form data).

function uploadFilesTo(url: string, files: File | File[], httpMethod: 'POST' | 'PUT' = 'POST'): Promise<Response>

Model

FilePickerOptions

NameTypeRequiredDefaultDescription
acceptedExtensionsstring[]NO""An array of unique file type specifiers, describing which file types to allow.

FileStringResult

NameTypeRequiredDefaultDescription
namestringYESundefinedThe name of the file.
sizenumberYESundefinedThe size of the file in bytes.
typestringYESundefinedThe MIME type of the file.
lastModifiednumberYESundefinedThe last modified time of the file, in millisecond since the UNIX epoch.
webkitRelativePathstringYESundefinedThe path the URL of the file is relative to.
contentstringYESundefinedThe string representation of the file's content

Changelog

Version 1.1:

  • now handles when the user does not select a file
  • now handles input errors
  • updated examples and documentation
  • updated TypeScript version

Version 1.0.4:

  • added example for Svelte
  • updated TypeScript version

Version 1.0.3:

  • added git repository
  • added FileStringResult type

Version 1.0.2:

  • added example for VueJS

Version 1.0.1:

  • fixed typo in documentation

Version 1.0:

  • published library

FAQs

No FAQs for now. (⌐■_■)

Keywords

FAQs

Package last updated on 21 Feb 2024

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc