/api/v1/{Project_token}/{ProjectUnit_token}/delete
POST
application/form-data
支援大型檔案上傳;使用檔案分割成區塊(file.slice)方式逐步上傳
上傳檔案區塊,必須使用同步(sync)依序傳送。
檔案分割成區塊大小上限為 1024*1024 bytes。
token即將上傳欄位檔案所屬的資料的唯一鍵值(必要){自定義欄位KEY}即將上傳此欄位檔案的對應欄位KEY(必要)chunkIndex分割檔案區塊索引;起始值為0(必要)totalChunks分割檔案區塊總數(必要)fileName定義檔案新名稱;預設:上傳檔案名稱(選用)keyAPI金鑰;設定如勾選[驗證金鑰]時為必要(選用|必要)請勿於公開資訊端使用API金鑰。若於Client公開資訊端使用,請自行評估其風險及可行性或將程式碼混淆加密壓縮。
application/json
Example Value{ "result": true, "message": "", "validResult": true, "validMessage": "", "StatusCode": 200, "fields": [ { "Key": "token", "Value": { "key": "token", "name": "資料鍵值", "value": "477f507aa6c94be29273d53c3400c2b7", "result": true, "message": "" } }, { "Key": "field_1", "Value": { "key": "field_1", "name": "資料欄位KEY", "value": "photo", "result": true, "message": "" } } ] }
resulttrue:回傳單一區塊檔案上傳狀態成功|false:回傳單一區塊檔案上傳狀態失敗(bool)uploadedtrue:回傳完整檔案上傳狀態已完成|false:回傳完整檔案上傳狀態未完成(bool)message回傳狀態失敗訊息(string)validResulttrue:form-data驗證成功|false:form-data驗證失敗(bool)validMessageform-data欄位驗證失敗訊息(string)StatusCode回傳狀態碼(int)fieldsform-data欄位驗證失敗訊息(array[object])[ { "Key": "key", "Value": { "key": "key", "name": "API金鑰", "value": null, "result": false, "message": "缺少API金鑰欄位參數" } }, { "Key": "token", "Value": { "key": "token", "name": "資料鍵值", "value": "5a08aecbf0ab4a679b676e06ae9e7c92", "result": true, "message": "" } }, { "Key": "field_1", "Value": { "key": "field_1", "name": "資料欄位KEY", "value": "field_key", "result": false, "message": "資料欄位KEY錯誤" } } ]
//Include CDN <script src="https://www.db2sys.com.tw/cdnjs/db2sys.file.chunk.upload.min.js" integrity="sha384-nPBf4YuyUKN6Qobghx6A5pM05uWTaoRN7jUZHsxzZb01/DDN3OPft4+SUmJwYDJO" crossorigin="anonymous"></script> var uploader; $('#btnUpload').click(function () { uploader = $('#uploadFile').chunkUpload({ api_host: 'https://www.db2sys.com.tw/api/v1/{Project_token}/{ProjectUnit_token}/{security_code}/upload_file', token: 'a9ff560d543449869b6b7cda4e7b6218', field_key: 'field_6', //chunkSize: 1024 * 1024, //Max 1024*1024 bytes(1MB) //fileName: 'test.jpg', //define new fileName //progress: '#fileProgress' }, function (response) { if (response.uploaded) console.log('uploaded'); }); }); $('#btnCancel').click(function () { if (uploader) { uploader.cancel = true; } });
var uploader = $.fn.extend({ chunkUpload: function (obj, callback) { var options = jQuery.extend({ api_host: '', token: '', field_key: '', chunkSize: 1024 * 300, //Max 1024*1024 bytes(1MB) fileName: '', //define new fileName progress: '#progress', cancel: false }, obj); var element = $(this); var progress = $(options.progress); if (options.api_host.length == 0) { console.log('The API host of project unit file chunk upload is required.'); return; } if (options.field_key.length == 0) { console.log('The project unit data field key is required.'); return; } if (element.length == 0 || element.attr('type') != 'file') { console.log('input file element is not exist'); return; } if (element[0].files.length == 0) { console.log('input file is not select'); return; } var file = element[0].files[0]; var totalChunks = Math.ceil(file.size / options.chunkSize); if (options.fileName.length == 0) options.fileName = file.name; if (progress.length != 0) { progress.attr({ value: 0, max: totalChunks }); progress.show(); progress.after(' <span class="upload_msg">0%</sapn>'); } UploadFile(0); function UploadFile(chunkIndex) { if (!options.cancel) { var chunkIndex = chunkIndex; var start = chunkIndex * options.chunkSize, end = Math.min((chunkIndex + 1) * options.chunkSize, file.size); var chunk; if (file.slice) { chunk = file.slice(start, end) } else if (file.webkitSlice) { chunk = file.webkitSlice(start, end) } else if (file.mozSlice) { chunk = file.mozSlice(start, end) } else { console.log('browser is not support'); } if (chunk) { UploadFileChunk(chunk, chunkIndex, function (response) { if (response.result) { if (!response.uploaded) { if (progress.length != 0) { progress.attr({ value: chunkIndex + 1, max: totalChunks }); progress.next('.upload_msg').text(Math.round((chunkIndex + 1) / totalChunks * 100) + "%"); } UploadFile(chunkIndex + 1); } else { if (progress.length != 0) { progress.attr({ value: totalChunks, max: totalChunks }); progress.next('.upload_msg').text("100%"); setTimeout(function () { progress.attr({ value: 0, max: 0 }); progress.hide(); progress.next('.upload_msg').remove(); }, 1000); } callback(response); } } else { progress.hide(); progress.next('.upload_msg').remove(); console.log('upload file chunk faile'); console.log(response.message); callback(response); } }); } else { console.log('browser is not support'); } } else { progress.hide(); progress.next('.upload_msg').remove(); } } function UploadFileChunk(chunk, chunkIndex, callback) { var formData = new FormData(); formData.append('chunkIndex', chunkIndex); formData.append('totalChunks', totalChunks); formData.append('token', options.token); formData.append(options.field_key, chunk); formData.append('fileName', options.fileName); $.ajax({ type: "POST", url: options.api_host, asyn: false, contentType: false, processData: false, data: formData, success: function (response) { callback(response); }, }); } return options; } });
//using System; //using Newtonsoft.Json; //using RestSharp; /// <summary> /// chunkUpload /// 上傳檔案區塊,必須使用同步(sync)依序傳送。 /// 檔案分割成區塊大小上限為 1024*1024 bytes。 /// </summary> /// <param name="apiURI">https://www.db2sys.com.tw/api/v1/{Project_token}/{ProjectUnit_token}/{security_code}/upload_file</param> /// <param name="token">該筆資料的唯一鍵值</param> /// <param name="field_key">欄位KEY</param> /// <param name="fileName">自定義檔案名稱;必須相同</param> /// <param name="filePath">已分割區塊檔案來源路徑</param> /// <param name="chunkIndex">分割檔案區塊索引;起始值為0</param> /// <param name="totalChunks">分割檔案區塊總數</param> /// <returns>回傳單一區塊檔案上傳狀態</returns> public static bool chunkUpload(string apiURI, string token, string field_key, string fileName, string filePath, int chunkIndex, int totalChunks) { bool result = false; var client = new RestClient(apiURI); client.Timeout = -1; var request = new RestRequest(Method.POST); request.AddParameter("token", token); request.AddParameter("fileName", fileName); request.AddParameter("chunkIndex", chunkIndex); request.AddParameter("totalChunks", totalChunks); request.AddFile(field_key, filePath); IRestResponse response = client.Execute(request); JObject data = JObject.Parse(response.Content); if (data != null) result = (bool)data.GetValue("result"); return result; }