1.1.0 • Published 2 years ago

@andreashauschild/just-upload v1.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

just-upload

Just Upload is an angular library (without external dependencies) that provides all necessary features to make file upload simple as possible. The goal of the library is to provide services , models and examples to implement your own custom upload components.

Requires: Angular 13+

Info The developer provides a backend service (quarkus) to execute real file uploads. Files send to this api will be deleted after upload. Endpoint: https://just-upload.litexo.de

system schema

Features

Installation

npm install @andreashauschild/just-upload

Usage

To use this library you need to import the JustUploadModule into your application. After that you can use the JustUploadService in your components.

Import the JustUploadModule

Import it in your angular app.module.ts. Furthermore, the HttpClientModule from @angular/common/http must also be imported.

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    HttpClientModule,
    JustUploadModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

Inject the Service in your Component

constructor(private service: JustUploadService)

Concepts - Basic Upload

The JustUploadService helps you to create an upload object which provides all necessary functions to implement your upload. To create an upload you need to provide a reference to a HTML file input element and a configuration.

After the creation of the upload object you can listen to changes if you subscribe to onFileProcessed and onFiledAdded.

The most simple upload looks like this (The code can be found here: example1):

Info the backend of the upload server is implemented in java with quarkus. You can find the REST-Endpoint here: BasicUploadResource.java

import {AfterViewInit, Component, ElementRef, ViewChild} from '@angular/core';
import {JustUploadService, Upload, UploadConfig} from 'just-upload';

@Component({
  selector: 'app-documentation1',
  template: `
    <!-- HTML File Input with template variables as reference    -->
    <input #fileUpload type="file">
  `,
  styles: [``]
})
export class Documentation1Component implements AfterViewInit {

  // Element reference of the file input field. Be aware that this ref will be accessible in AfterViewInit
  @ViewChild("fileUpload", {static: false})
  fileUpload: ElementRef | undefined;

  // The config for the upload
  config: UploadConfig;

  // The upload object which controls everything
  upload?: Upload;

  constructor(private service: JustUploadService) {
    // initialize basic config this will send the file immediately to the given endpoint with a post request
    this.config = {
      url: "https://just-upload.litexo.de/api/basic-upload/single-binary",
      method: "POST"
    }
  }

  // Use ngAfterViewInit because in the lifecycle step the 'upload' is set
  ngAfterViewInit(): void {

    // Setup the upload
    this.upload = this.service.createUpload(this.fileUpload!, this.config);

    // Log the processing stages of the file
    this.upload.onFileProcessed().subscribe(procced => {
      console.log("File Processed", procced);
    });

    // Log if a file was added to the upload
    this.upload.onFiledAdded().subscribe(added => {
      console.log("File Added", added);
    });
  }

}

UploadConfig - UploadFile - Upload & MultipartFormUpload

UploadConfig

The UploadConfig allows the following configurations:

AttributeDescription
urlhttp endpoint of the upload
uploadImmediatelyif true files will immediately be uploaded after there added to the upload default: false
methodhttp request method that should be used (POST, PUT, PATCH)
maxFileSizemaximal size of a file that can be added default: 100MB
multifalse = single file upload; true = multi file upload
acceptaccept value of the input field e.g. *, png jpeg jpg, see: input accept
beforeFileSendHook?(before: BeforeFileSend): RequestParams;callback function that executes before the file is send. this is used to add custom header or query parameters to the request. These custom parameters must be returned as result of the callback function
afterFileSendHook?(after: AfterFileSend): void;callback function that executes after the file is send

UploadFile

The UploadFile is the file data model. It holds the browser file and additional information:

AttributeDescription
filereference to the original file object
namename of the file
extensionextension of the file e.g.: png, jpg
sizesize in bytes of the file
sizeHumanReadablesize of file as human readable string like: 100kb, 2MB, 1GB
mimeTypeMimeType of the file see MimeTypes.ts
fileIdid of the file that is uploaded. This is generated: like 128_thumbnail.png_1640697283494
statecurrent upload state of the file see UploadState
loadedHumanReadablecurrent value of uploaded data as human readable string
loadedPercenttotally transferred of file in percent
httpResponsestores the httpResponse of the upload. can be used for specialized response handling. can be HttpResponse, HttpErrorResponse or undefined if the file was not send yet

Upload & MultipartFormUpload

The Upload and MultipartFormUpload will be created by a factory method of the JustUploadService. It is the main object you will work with. In case of MultipartFormUpload every file will be sent as single multipart/form-data request. This means if you have 5 files in your file list, they will be sent in 5 separate requests. If you use Upload every file will be sent as an application/octet-stream to the server.

MethodsDescription
uploadFile(uploadFile: UploadFile): voidMethod to trigger the upload process of the given file. The method can be used if uploadImmediately is disabled
onFileProcessed(): Observable<UploadFile>Subscribe for receiving progress chances while the file is processed. ReturnsUploadFile with the current state. Info: If you are uploading files with a very good connection (e.g. local server) you may get only one processing update
onFiledAdded(): Observable<UploadFile>Subscribe for receiving the files that are added to the input element
addFile(file: File): voidAdds the File): to the upload. This can be used add files programmatically if needed. For example if you build drag and drop components.
addFiles(fileList: FileList): voidAdds the FileList to the upload. This can be used add files programmatically if needed. For example if you build drag and drop components.

ChunkedUploadConfig - ChunkedUploadFile - ChunkedUpload

Chunked Upload - Splits a file in chunks and sends for each chunk a http request. This can be used to upload very large files which may exceeding the size of a single request.

The ChunkedUploadConfig allows the following configurations:

AttributeDescription
urlhttp endpoint of the upload
uploadImmediatelyif true files will immediately be uploaded after there added to the upload default: false
methodhttp request method that should be used (POST, PUT, PATCH)
maxFileSizemaximal size of a file that can be added default: 100MB
multifalse = single file upload; true = multi file upload
acceptaccept value of the input field e.g. *, png jpeg jpg, see: input accept
chunkSizesize of a chunk in bytes
beforeChunkSendHook?(before: BeforeChunkSend): RequestParams;callback function to intercept and set parameters (e.g. header and query params) before the chunk is send to server
afterChunkSendHook?(after: AfterChunkSend): void;callback function to intercept if the chunk was send and the server response was received

ChunkedUploadFile & FileChunk

The ChunkedUploadFile represents the file that is uploaded an FileChunk represents a chunk of this file. It holds the browser file and additional information:

ChunkedUploadFile

AttributeDescription
filereference to the original file object
namename of the file
extensionextension of the file e.g.: png, jpg
sizesize in bytes of the file
sizeHumanReadablesize of file as human readable string like: 100kb, 2MB, 1GB
mimeTypeMimeType of the file see MimeTypes.ts
fileIdid of the file that is uploaded. This is generated: like 128_thumbnail.png_1640697283494
statecurrent upload state of the file see UploadState
loadedHumanReadablecurrent value of uploaded data as human readable string
loadedPercenttotally transferred of file in percent
httpResponsestores the httpResponse of the upload. can be used for specialized response handling. can be HttpResponse, HttpErrorResponse or undefined if the file was not send yet
totalChunksTotal number of chunks that will be read, based on the file size and maximum chunk size
maxChunkSizeMaximum size of a chunk
currentChunkProvides the current chunk that will be send

ChunkedUploadFile

AttributeDescription
uploadFilereference to the owning ChunkedUploadFile
loadedPercentuploaded percent of the file with this chunk e.g file with 10 chunks and current chunk is number 5 it will be 50
loadedHumanReadableuploaded bytes of the file with this chunk
finisheddata of the chunk bytes
chunk.datadata of the chunk bytes
chunk.indexindex of the chunk from 0..n
chunk.sizesize of the chunk
chunk.offsetcurrent offset of this chunk

ChunkedUpload

The ChunkedUpload will be created by a factory method of the JustUploadService. It is the main object you will work with. Every chunk will be sent as an application/octet-stream to the server.

AttributeDescription
onChunkProcessed(): Observable<ChunkedUploadFile>Subscribe to this function to handle chunks after there where send to server
onFiledAdded(): Observable<ChunkedUploadFile>Subscribe for receiving the files that are added to the input element
uploadFile(file: ChunkedUploadFile): voidMethod to trigger the upload process of the given file. The method can be used if uploadImmediately is disabled
addFile(file: File): voidAdds the File): to the upload. This can be used add files programmatically if needed. For example if you build drag and drop components.
addFiles(fileList: FileList): voidAdds the FileList to the upload. This can be used add files programmatically if needed. For example if you build drag and drop components.
1.1.0

2 years ago

1.0.0

2 years ago

0.0.7

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago