Hi,
I am getting an error as Guid should contain 32 digits with 4 dashes (xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx) while uploading a file in Document library via webpart by JSOM.
Below is the Code that I am using to upload file in chunks.
that.insertDocument = function (file, Id, docType, resolutionId, TicketNumber, handleLoader = true) {
// Generate a new GUID string
var newGuid = SP.Guid.newGuid().toString();
console.log(newGuid);
// Function to create a GUID string in the correct format
function createGuid() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxx'.replace(/[xy]/g, function (c) {
var r = Math.random() * 16 | 0,
v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
// Get the context and the SharePoint list where you want to upload the file
var clientContext = SP.ClientContext.get_current();
var web = clientContext.get_web();
clientContext.load(web, 'ServerRelativeUrl');
clientContext.executeQueryAsync(
async function () {
console.log
var folderServerRelativeUrl = web.get_serverRelativeUrl() + '/ArcilDocuments';
var docLib = web.getFolderByServerRelativeUrl(folderServerRelativeUrl);
console.log(folderServerRelativeUrl);
var chunkSize = 1024 * 1024;
//var fileInput = document.getElementById('fileInput');
//var file = fileInput.files[0];
//debugger
// Calculate the number of chunks
var fileSize = file.size;
var numChunks = Math.ceil(fileSize / chunkSize);
var fileName = file.name;
var names = fileName.split('.');
var ext = names.pop();
var newName = TicketNumber.replace(/\//g, '-') + '-' + names[0] + '-' + new Date().getTime() + '.' + ext;
console.log(newName);
// Create a new file creation information object
var fileCreateInfo = new SP.FileCreationInformation();
console.log("fileCreateInfo object:", fileCreateInfo);
fileCreateInfo.set_url(newName);
console.log("fileCreateInfo after set_url():", fileCreateInfo);
fileCreateInfo.set_overwrite(true);
console.log("fileCreateInfo after set_overwrite():", fileCreateInfo);
fileCreateInfo.set_content(new SP.Base64EncodedByteArray());
console.log("fileCreateInfo after set_content():", fileCreateInfo);
console.log(fileCreateInfo);
//debugger
// Upload the file to SharePoint
var newFile = docLib.get_files().add(fileCreateInfo);
var listItem = newFile.get_listItemAllFields();
listItem.set_item('Year', new Date().getFullYear());
listItem.set_item('TicketId', Id);
listItem.set_item('DocumentType', docType);
listItem.set_item('UploadedOn', new Date());
listItem.set_item('ResolutionId', resolutionId);
listItem.set_item('DocumentStatus', true);
listItem.update();
// Upload the file in chunks
var chunkIndex = 0;
var offset = 0;
var chunkReader = new FileReader();
chunkReader.onload = async function (evt) {
if (evt.target.error == null) {
var buffer = evt.target.result;
var endpoint = offset + buffer.byteLength - 1;
var chunkStream = new SP.Base64EncodedByteArray();
chunkStream.append(buffer);
var parameters = new SP.FileCreationInformation();
parameters.ContentStream = chunkStream;
parameters.FileName = newName;
parameters.Offset = offset;
parameters.OverwriteIfExists = true;
//debugger
// Start uploading the chunk
if (chunkIndex == 0) {
console.log(newGuid);
console.log("parameters object:", parameters);
// Start uploading the chunk
newFile.startUpload(parameters, newGuid);
console.log('Upload start');
} else {
//var chunkStream = new SP. Base64EncodedByteArray();
//chunkStream.append(buffer);
newFile.continueUpload(chunkStream);
console.log('upload continuously')
}
// Move to the next chunk or finish uploading
chunkIndex++;
offset += chunkSize;
if (offset < fileSize) {
readNextChunk();
} else {
newFile.finishUpload();
console.log('Upload completed');
clientContext.load(newFile);
await clientContext.executeQueryAsync(
function () {
var fileUrl = uploadedFile.get_serverRelativeUrl();
if (handleLoader == true) {
commonHelper.showHideLoader(false);
}
//console.log('File uploaded successfully - ' + fileUrl);
deferred.resolve(fileUrl);
},
function (sender, args) {
if (handleLoader == true) {
commonHelper.showHideLoader(false);
}
console.log('Error uploading file: ' + args.get_message());
deferred.resolve('');
}
);
}
}
}
var readNextChunk = function () {
var start = offset;
var end = Math.min(start + chunkSize, fileSize);
chunkReader.readAsArrayBuffer(file.slice(start, end));
};
readNextChunk();
})
};