What is the best way to insert multiple files

What is the best way to insert multiple files, since in my stream it can return to the user to re-insert the files and the way I do it sometimes it changes the contents, the contents of file 1 are in file 2 and vice versa it seems that does not synchronize the index with the file name. I’ll put the code I use to include multiple files

      		$scope.files = [];
      		$scope.upload = function(fileUpload) {
     		   	for (let i = 0; i < fileUpload.files.length; i++) {
					let name = fileUpload.files[i].name
					let ext = name.split('.')
					ext = ext[ext.length - 1]
     		   		let file = { name: fileUpload.files[i].name, extensao: ext, mimetype: fileUpload.files[i].type, data: undefined, blob: undefined };
        		}
    	    	$scope.form.$setDirty();
	        	$scope.$apply();
      		};
      
     		$scope.retrieveFiledata = function (file, fileData) {
        		let reader = new FileReader();
        		reader.onload = function (event) {
          			fileData.blob = reader.result;
          			let binary = '';
          			let bytes = new Uint8Array(reader.result);
          			for (let i = 0; i < bytes.length; i++) {
            			binary += String.fromCharCode(bytes[i]);
          			}
          			fileData.data = btoa(binary);
        		};
        		reader.readAsArrayBuffer(file);
      		};

      		$scope.removeDocument = function (file) {
        		$scope.files.splice($scope.files.indexOf(file), 1);
      		};
		}]) 

camForm.on ('submit',function () { 
$scope.files.forEach(function (file, index) {
    	  camForm.variableManager.createVariable({
             name: 'sys_file_'+ index,
           	type: 'Bytes',
           	value: file.data
       	});
	 camForm.variableManager.createVariable({
        	 name: 'sys_filename_' + index,
       		type: 'String',
           	value: file.name,
		isDirty: true
       	});
	camForm.variableManager.createVariable({
        	name: 'sys_ext_' + index,
           	type: 'String',
           	value: file.extensao
   	   });
   });
}```


In the following form to get these attachments I use the following code

``` inject(['$http', 'Uri', function($http, Uri) {
				var res = []
       			$http.get(Uri.appUri("engine://engine/:engine/task/" + camForm.taskId + "/variables/sys_qtd_arquivos")).success(function(result){
          			var count = result.value
					var j = 0
					var table = document.createElement('table')
					table.style = 'margin-bottom:20px';
					var tbody = document.createElement('tbody')
					console.log(camForm.taskId)
					for (var i=0; i<count; i++){
						$http.get(Uri.appUri("engine://engine/:engine/task/" + camForm.taskId + "/variables/sys_filename_"+i))
							.success(function(result){
								res[i] = result.value
								let tr = document.createElement('tr');
								
								let td = document.createElement('td');
							    let a = document.createElement('a');
    								a.setAttribute("cam-file-download","sys_file_"+j);
									a.setAttribute("href", "/camunda/api/engine/engine/default/task/"+camForm.taskId+"/variables/sys_file_"+j+"/data")
									a.setAttribute("class", "glyphicon glyphicon-file form-control")
									a.setAttribute("style", "border:none; color:blue")
									a.setAttribute("download", res[i])
									a.innerHTML = res[i]
    							td.appendChild(a);
    							tr.appendChild(td);
	
								tbody.appendChild(tr);
								j = j+1
							})
					}
					table.appendChild(tbody);
					document.getElementById('tabela').appendChild(table);
        		});
    		}]);     ```

the error in the code was to assign the result of the call to api in a variable of type array → res [i] the correct one is a simple variable → res

1 Like