Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
This repository was archived by the owner on Apr 26, 2022. It is now read-only.
/cj-uploadPublic archive

Higher order React components for file uploading (with progress) react file upload

License

NotificationsYou must be signed in to change notification settings

kimmelsg/cj-upload

Repository files navigation

Please look atreact-use-upload for the same functionality, updated using React hooks.

CircleCICoverage Status

@navjobs/upload

v4

I'm working on a v4 soon that simplfies the api and removes the children-as-a-function paradigm to something more extendable. Also revamping the test suit. It'll be a complete rewrite.

What

A set of React components for handling file uploads. If you simply want to turn any component into a file upload dialog, wrap it in our<UploadField/> component that exposes the files after selection. Need to process a file upload and receive the upload progress? Wrap<UploadField/> with<Uploader/>. You can see examples insideour storybook.

Why this?

  • Any component can be an upload dialog. Wrap it in<UploadField/>. This means you have ultimate styling control.
  • Simple component API for upload progress. Pass headers, extra fields, anything.
  • Zero dependencies (other than React!)

Install

yarn add @navjobs/upload

In this library

  • UploadField gives access to files after drag and drop / clicking on the area wrapped
  • Uploader triggers an xhr upload to a url with file progress
  • SignedUploader same as above, but helps generate asigned url from your api.
  • Imperative api that lets you trigger a file upload with progress outside of react.

UploadField

import{UploadField}from'@navjobs/upload'<UploadFieldonFiles={files=>//files object here}containerProps={{className:'resume_import'}}uploadProps={{accept:'.pdf,.doc,.docx,.txt,.rtf',}}><div>      Click here to upload! This can be an image,      or any component you can dream of.</div></UploadField>

Uploader

Use<UploadField /> inside of this component; pass the files to it and handle the upload!

import{Uploader}from'@navjobs/upload'<Uploaderrequest={{fileName:'file',url:'https://upload.com',method:'POST',fields:{//extra fields to pass with the requestfull_name:'Testing extra fields',},headers:{//custom headers to send alongAuthorization:'Bearer: Test',},// use credentials for cross-site requestswithCredentials:false,}}onComplete={({ response, status})=>/*do something*/}//upload on file selection, otherwise use `startUpload`uploadOnSelection={true}>{({ onFiles, progress, complete})=>(<div><UploadFieldonFiles={onFiles}><div>          Click here to select a file!</div></UploadField>{progress ?`Progress:${progress}` :null}{complete ?'Complete!' :null}</div>)}</Uploader>

Signed Uploader

This is a useful component for generating signed urls on your backend for a service likeAWS orGoogle Cloud.The workflow generally involes hitting your own api, then uploading to the url that your api returns. After the fact, you hit your api again to say that the upload is finished.

import{SignedUploader}from'@navjobs/upload'<SignedUploader//grab this url from your apibeforeRequest={()=>newPromise(resolve=>resolve({url:'http://storage.googlecloud.com'}))}request={({ before, files})=>({url:before.url,method:'PUT',headers:{'Content-Type':files[0].type}})}afterRequest={({ before, status})=>newPromise(resolve=>{resolve('finished the upload!');})}>

Imperative API

If you need to upload files and recieve progress, but can't wrap anUploader around where you receive the files, feel free to use this:

import{UploadRequest}from'@navjobs/upload'asyncuploadFiles(){let{ response, error, aborted}=awaitUploadRequest({request:{url:'blah'//same as above request object},      files,//files arrayprogress:value=>console.log('progress!',value)})//do something with response}

FAQ

Q: Part of the component I'm wrapping isn't cursor pointer?

A: You may need to set

::-webkit-file-upload-button {cursor:pointer; }

In your css. For some reason file uploads aren't always pointer.

License

This project is licensed under the terms of theMIT license.


[8]ページ先頭

©2009-2025 Movatter.jp