From 8ae876445ed94661214fe94a49dd83dbb6cb8949 Mon Sep 17 00:00:00 2001 From: Paulo Date: Fri, 9 Mar 2018 10:22:26 -0300 Subject: [PATCH] Improving ReadME --- README.md | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 23e3175..7fb3c62 100644 --- a/README.md +++ b/README.md @@ -89,12 +89,12 @@ Please follow this guidelines when reporting bugs and feature requests: Thanks for understanding! -### Using/Sending Chunk Files Feature +## Using/Sending Chunk Files Feature If you want to send the files chunked you can just set the chunk paramets on the uploader object If your chunk request changes the link after the first request you should use this code - ```txt + ```typescript this.uploader.onCompleteChunk = (item,response,status,headers)=>{ response = JSON.parse(response); if(response['id']){ @@ -103,6 +103,53 @@ Thanks for understanding! } ``` +### Code snippet on how to use the Chunk File Feature on your code + ```typescript + ... + import { FileUploader } from 'ng2-file-upload'; + ... + export class SimpleDemoComponent { + ... + uploader:FileUploader; + ... + constructor () { + ... + this.uploader = new FileUploader({ + url: URL, + disableMultipart : false, + isHTML5: true, + chunkSize: (1024*1024), // 2MB + currentChunkParam: 'current_chunk', + totalChunkParam: 'total_chunks', + chunkMethod: 'PUT', + //authToken = 'JWT '+TOKEN, + }); + this.uploader.onBeforeUploadItem = (item) => { + // If you use credentials this might help you with the "Access-Control-Allow-Origin" error + item.withCredentials = false; + }; + this.uploader.onCompleteChunk = (item, response, status, headers) => { + //Insert the Logic here to start uploading next chunks + // Example, setting the ID of the File uploaded and chaning the link for the next request + // In my Case the API is using a put method with the link containing the PK of the object + response = JSON.parse(response); + if (response['id']) { + item.setId(response['id']); + item.url = this.media_url + item.getId() + '/'; + } + }; + this.uploader.onErrorItem = (item, response, status, headers) => { + // Treat the error on the upload + // On the chunk method we try to upload a chunk for 10 times before triggering this error + }; + this.uploader.onRemoveItem = (item) => { + // Treat the file removal from the server + }; + ... + } + ``` + + ### License The MIT License (see the [LICENSE](https://github.com/valor-software/ng2-file-upload/blob/master/LICENSE) file for the full text)