Socket
Socket
Sign inDemoInstall

@bytesoftio/helpers-input

Package Overview
Dependencies
1
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @bytesoftio/helpers-input

## Installation


Version published
Weekly downloads
2
Maintainers
1
Install size
25.1 kB
Created
Weekly downloads
 

Readme

Source

@bytesoftio/helpers-input

Installation

yarn add @bytesoftio/helpers-input or npm install @bytesoftio/helpers-input

Table of contents

  • Description
  • Examples
  • Usage

Description

This package provides several helpers to deal with file inputs.

Examples

Submit a file to the server

Submit a file immediately after selecting one.

import { selectFile } from "@bytesoftio/helpers-input"
import axios from "axios"

const handleSelectFile = async () => {
  const file = await selectFile()
  
  if ( ! file) return

  submitFile(file) 
}

const submitFile = async (file: File) => {
  const formData = new FormData()
  formData.append('file', file)

  await axios.post(`/endpoint`, formData, {
    headers: { 'content-type': 'multipart/form-data' },
  })
}


<button onClick={handleSelectFile}/>

Show a file preview

Show a file preview immediately after selection.

import { selectFileOfType } from "@bytesoftio/helpers-input"

const handleSelectFileOfType = async () => {
  const file = await selectFileOfType('image/*')

  if ( ! file) return

  showPreview(file)
}

const showPreview = (file: File) => {
  const img = document.createElement('img')
  img.src = URL.createObjectURL(file)
  document.body.appendChild(img)
}

<button onClick={handleSelectFileOfType}/>

Usage

selectFile

Let the user select a single file.

import { selectFile } from "@bytesoftio/helpers-input"
import axios from "axios"

const handleSelectFile = async () => {
  const file = await selectFile()
  
  if ( ! file) return

  // do something with the file...
}

<button onClick={handleSelectFile}/>

selectFiles

Similar to selectFile, but allows user to select multiple files.

import { selectFiles } from "@bytesoftio/helpers-input"

const handleSelectFiles = async () => {
  const files = await selectFiles()

  if ( ! files) return

  // do something with the files...
}

<button onClick={handleSelectFiles}/>

selectFileOfType

Allow only specific types of files, used inputs accept property behind the scenes, see here.

import { selectFileOfType } from "@bytesoftio/helpers-input"

const handleSelectFileOfType = async () => {
  const file = await selectFileOfType('image/*')

  if ( ! file) return

  // do something with the file...
}

<button onClick={handleSelectFileOfType}/>

selectFilesOfType

Similar to selectFileOfType, but allows user to select multiple files.

import { selectFilesOfType } from "@bytesoftio/helpers-input"

const handleSelectFilesOfType = async () => {
  const files = await selectFilesOfType('image/*')

  if ( ! files) return

  // do something with the files...
}

<button onClick={handleSelectFilesOfType}/>

FAQs

Last updated on 16 Dec 2020

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

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

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc