Socket
Book a DemoInstallSign in
Socket

bureaucracy

Package Overview
Dependencies
Maintainers
1
Versions
8
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

bureaucracy

Pull files from hidden file inputs into text inputs with ease

latest
Source
npmnpm
Version
1.0.7
Version published
Maintainers
1
Created
Source

bureaucracy

Pull files from hidden file inputs into text inputs with ease

If you want the hidden file inputs part, just absolutely position the file input behind a button skin. Many articles on the internet explain how to do this.

install

npm i bureaucracy -S

create(options?)

Creates a bureaucrat object that can submit files to an HTTP API endpoint. It takes a few options.

OptionDescription
validateFunction that receives a File object and should indicate whether that file is valid
methodHTTP method to use when posting the files. Defaults to PUT
endpointHTTP endpoint to post the files to. Defaults to /api/files
fieldKeyString setting file upload field key. Defaults to uploads
formDataObject containing additional form parameters. Defaults to an empty object: {}

There are "common" validate functions for your convenience. These can be referenced by name on the validate option

ValidatorDescription
'image'Files are expected to have a MIME type of image/gif, image/png, image/jpg, or image/jpeg

bureaucrat.submit(files)

The files parameter expects an array of File or a FileList, like the ones you can pull from input.files, where input is an input element of type file.

The files are uploaded using a default uploads field key, but this can be customized using the fieldKey options parameter. Once your API has handled the uploaded files, a JSON response should be returned. The only requirement here is a results property that's an array describing the success of each file upload.

{
  "results": [
    "anything",
    { "can": "be_used"},
    ["as", "a", "result"]
  ]
}

This data can be useful for the front-end to react to successful file uploads on the success event.

var bureaucrat = create();
bureaucrat.on('success', uploadSuccess);
bureaucrat.submit(uploadInput.files);

function uploadSuccess (results) {
  console.log('so many files were uploaded!');
}

The submission process emits a variety of events on the bureaucrat object, using the contra emitter API.

EventArgumentsDescription
startedrawFilesA submission attempt has started for rawFiles
invalidallFilesSubmitted files were invalid, a request won't be made
validvalidFilesAt least one submitted file was valid, a request will be made
errorerrAn error err occurred during the HTTP API request
successresults, bodyThe file API call succeeded and yielded some results
endederr, results, bodyThe HTTP API request process ended, emitted after both error and success

setup(fileinput, options?)

Reacts to change events on a file input by making a PUT /api/files request with the valid user-selected files as soon as the user is finished choosing their files.

license

mit

FAQs

Package last updated on 22 Aug 2016

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