I am trying to upload multiple files using Chatter REST API. I was previously able to upload a file. Now I have to upload multiple files. I got these file names and parameter names in array, as stated here: JS for uploading multiple files.
Code which worked for single file upload is as below:
var fileName = file.name;
var fileUrl = chatterUrl + "/users/me/files?title=" + fileName;
var formData = new FormData();
formData.append('fileData', file);
console.log('fileName ------>> '+fileName);
console.log('fileUrl ------>> '+fileUrl);
parent.ajaxCall = $.ajax({
type: "POST",
url: fileUrl,
contentType: false,
processData: false,
cache: false,
data: formData,
xhr: function() {
var xhr = new window.XMLHttpRequest();
xhr.upload.addEventListener("progress", function(evt) {
if (evt.lengthComputable) {
var percentComplete = evt.loaded / evt.total;
var pct = Math.round(percentComplete * 100);
var progressNode = $("progress", $this);
progressNode.val(pct);
progressNode.text(pct + " %");
}
}, false);
return xhr;
},
success: function(data) {
console.log(fileName + ', File ID: ' + data.id);
parent.successAction(data.id);
},
error: function(jqXHR, textStatus, errorThrown) {
console.log('File upload: ' + textStatus + ', ' + errorThrown + ', ' + jqXHR.responseText);
if (textStatus != 'abort')
alert('Something went wrong! Please try loading again ' + jqXHR.responseText);
}
});
But when I try to pass, file names, I am getting error.
This is how I tried to pass fileNames to REST API of chatter.
data: {paramNames:fileNames},
Best Answer
I solved my problem, by sending single file's multiple requests. AFAIK, REST API of Salesforce doesn't allow passing the files at once. So I sent single file request multiple times, so as to get my work done.