mirror of
				https://github.com/paperless-ngx/paperless-ngx.git
				synced 2025-11-03 03:16:10 -06:00 
			
		
		
		
	file upload improvements
This commit is contained in:
		@@ -6,7 +6,6 @@ import { Toast, ToastService } from 'src/app/services/toast.service';
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
interface UploadStatus {
 | 
			
		||||
  file: string
 | 
			
		||||
  loaded: number
 | 
			
		||||
  total: number 
 | 
			
		||||
}
 | 
			
		||||
@@ -30,11 +29,12 @@ export class UploadFileWidgetComponent implements OnInit {
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  uploadStatus: UploadStatus[] = []
 | 
			
		||||
  completedFiles = 0
 | 
			
		||||
 | 
			
		||||
  uploadVisible = false
 | 
			
		||||
 | 
			
		||||
  get loadedSum() {
 | 
			
		||||
    return this.uploadStatus.map(s => s.loaded).reduce((a,b) => a+b, 1)
 | 
			
		||||
    return this.uploadStatus.map(s => s.loaded).reduce((a,b) => a+b, this.completedFiles > 0 ? 1 : 0)
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  get totalSum() {
 | 
			
		||||
@@ -44,32 +44,35 @@ export class UploadFileWidgetComponent implements OnInit {
 | 
			
		||||
  public dropped(files: NgxFileDropEntry[]) {
 | 
			
		||||
    for (const droppedFile of files) {
 | 
			
		||||
      if (droppedFile.fileEntry.isFile) {
 | 
			
		||||
        const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
 | 
			
		||||
      let uploadStatusObject: UploadStatus = {loaded: 0, total: 1}
 | 
			
		||||
      this.uploadStatus.push(uploadStatusObject)
 | 
			
		||||
      this.uploadVisible = true
 | 
			
		||||
 | 
			
		||||
      const fileEntry = droppedFile.fileEntry as FileSystemFileEntry;
 | 
			
		||||
        fileEntry.file((file: File) => {
 | 
			
		||||
          let formData = new FormData()
 | 
			
		||||
          formData.append('document', file, file.name)
 | 
			
		||||
 | 
			
		||||
          let uploadStatusObject: UploadStatus = {file: file.name, loaded: 0, total: 1}
 | 
			
		||||
          this.uploadStatus.push(uploadStatusObject)
 | 
			
		||||
          this.uploadVisible = true
 | 
			
		||||
          this.documentService.uploadDocument(formData).subscribe(event => {
 | 
			
		||||
            if (event.type == HttpEventType.UploadProgress) {
 | 
			
		||||
              uploadStatusObject.loaded = event.loaded
 | 
			
		||||
              uploadStatusObject.total = event.total
 | 
			
		||||
            } else if (event.type == HttpEventType.Response) {
 | 
			
		||||
              this.uploadStatus.splice(this.uploadStatus.indexOf(uploadStatusObject), 1)
 | 
			
		||||
              this.completedFiles += 1
 | 
			
		||||
              this.toastService.showToast(Toast.make("Information", "The document has been uploaded and will be processed by the consumer shortly."))
 | 
			
		||||
            }
 | 
			
		||||
            
 | 
			
		||||
          }, error => {
 | 
			
		||||
            this.uploadStatus.splice(this.uploadStatus.indexOf(uploadStatusObject), 1)
 | 
			
		||||
            this.completedFiles += 1
 | 
			
		||||
            switch (error.status) {
 | 
			
		||||
              case 400: {
 | 
			
		||||
                this.toastService.showToast(Toast.makeError(`There was an error while uploading the document: ${error.error.document}`))
 | 
			
		||||
                break;
 | 
			
		||||
              }
 | 
			
		||||
              default: {
 | 
			
		||||
                this.toastService.showToast(Toast.makeError("An error has occured while uploading the document. Sorry!"))
 | 
			
		||||
                this.toastService.showToast(Toast.makeError("An error has occurred while uploading the document. Sorry!"))
 | 
			
		||||
                break;
 | 
			
		||||
              }
 | 
			
		||||
            }
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user