- Notifications
You must be signed in to change notification settings - Fork1
Datafruit/file-reader
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
TheFileReader class provides native file-readingcapabilities for modern browsers.
Sometimes we will read out the needs from large files.such as read a part of the text file to display on the browser.At this point need to read the file as fragmented to avoid the browser stuck.
FileReader is an asynchronous interface,so in the concurrent read need a program to manage these concurrent,pass the contents of the slices to the next operation with order.
You can wait for a slice finished then reading the next piece.becauseJavaScript is a single-threaded language, so the two ways notessentially different.
npm install next-reader --save-dev
All Reader extends ofBaseReader
- BrowserFileReader(file [,options])
- ReadLineAsUint8(file [,options])
- ReadLineAsString(file [,options])
- ReadCsvAsUint8(file [,options])
- ReadCsvWithLines(file, lines, [,options])
- {String} encode - default
utf8 - {Number} read_size - default
file.size - {Number} concurrency - default
1 - {Number} chunk_size - file's encode. default
utf8, only supportutf8,default1 << 16
- {File} file
- {ReaderOptions} [options]
import{BrowserFileReader}from'next-reader'consttarget=document.querySelector('#file')target.onchange=()=>{constfile=target.files[0]constreader=newBrowserFileReader(file)reader.subscribe((data,sequence)=>console.log('onNext::',data,sequence),(error,already,read_size)=>console.log('onError::',error,already,read_size),()=>console.log('onCompleted'))reader.read()// same as reader.readAsArrayBuffer()// orreader.readAsArrayBuffer()// orreader.readAsText()// orreader.readAsDataURL()}
MixinReaderOptions
- {Boolean} ignore_line_break - default
true
- {File} file
- {LineReaderOptions} [options]
import{ReadLineAsUint8}from'next-reader'constType=ReadLineAsUint8.Typeconsttarget=document.querySelector('#file')target.onchange=()=>{constfile=target.files[0]constreader=newReadLineAsUint8(file)reader.subscribe((record)=>{const{type, no, size, data}=recordif(type===Type.lines){console.log(data.forEach(d=>console.log(d)))}elseif(type===Type.line){console.log(data,no,size)}},(error,already,read_size)=>console.log('onError::',error,already,read_size),()=>console.log('onCompleted'))reader.read()}
- {File} file
- {LineReaderOptions} [options]
Simple is same asReadLineAsUint8
- {File} file
- {LineReaderOptions} [options]
import{ReadCsvAsUint8}from'next-reader'consttarget=document.querySelector('#file')target.onchange=()=>{constfile=target.files[0]constreader=newReadCsvAsUint8(file)reader.subscribe((record)=>{const{ lines, size}=recordlines.forEach(record=>{console.log(record.fields,record.size,record.no)})console.log(size)},(error,already,read_size)=>console.log('onError::',error,already,read_size),()=>console.log('onCompleted'))reader.read()}
- {File} file
- {Number} lines - lines to read
- {LineReaderOptions} [options]
import{ReadCsvWithLines}from'next-reader'consttarget=document.querySelector('#file')target.onchange=()=>{constfile=target.files[0]constreader=newReadCsvWithLines(file,100)reader.subscribe((record)=>{const{ lines, size}=recordlettotal=0lines.forEach(record=>{console.log('No. %s: %s .%s',record.no,record.fields.toString(),record.size)})console.log('total => %s, size => %s',total,size)},(error,already,read_size)=>console.log('onError::',error,already,read_size),()=>console.log('onCompleted'))reader.read()}
BaseReader extends ofBaseObservableand implementsAbstractReader
ImplementsAbstractObservable
- read()
- pause()
- resume()
- enqueue()
- onReadData()
- onReadError(error, already, read_size)
- onReadComplete()
- readable()
- validate(data)
- result(data)
Implements ofAbstractObservable
- subscribe(BaseObserverOrOnNext, onError, onComplete)
- subscribeOnNext(...args)
- subscribeOnError(...args)
- subscribeOnComplete(...args)
ImplementsAbstractObserver
- onNext(...args)
- onError(...args)
- onComplete(...args)
- destroy()
- isDestroy()
MIT
About
Base classes for read big file write by JavaScript
Topics
Resources
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.