-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Closed
Labels
Description
I created a function to read a Zip folder and upload in a Document Library Using jszip old version.
Now I want to Upgrade it to jszip 3.1.2
function jspres_importPptxPresentation_handleFileSelect(evt) {
var $result = $("#result");
$result.html("");
// see http://www.html5rocks.com/en/tutorials/file/dndfiles/
var files = evt.target.files;
for (var i = 0, f; f = files[i]; i++) {
var reader = new FileReader();
// Closure to capture the file information.
reader.onload = (function (theFile) {
return function (e) {
var $title = $("<h3>", {
text: theFile.name
});
$result.append($title);
var $ul = $("<ul>");
try {
var dateBefore = new Date();
// read the content of the file with JSZip
//-----------------------------------------------------------------------------------------------
var zip = new JSZip(e.target.result); //----------------<1>-----
//----------------------------------------------------------------------------------------------
var dateAfter = new Date();
$title.append($("<span>", {
text: " (parsed in " + (dateAfter - dateBefore) + "ms)"
}));
// that, or a good ol' for(var entryName in zip.files)
$.each(zip.files, function (index, zipEntry) {
var reader1 = new FileReader();
$ul.append("<li>" + zipEntry.name + "</li>");
if (!zipEntry.options.dir)
{
//---------------------------------------------------------------------------------------------------------
var addFile = addFileToFolder(zipEntry.asArrayBuffer(), zipEntry.name); //---<2>
//---------------------------------------------------------------------------------------------------
addFile.done(function (file, status, xhr) {
// Get the list item that corresponds to the uploaded file.
var getItem = getListItem(file.d.ListItemAllFields.__deferred.uri);
getItem.done(function (listItem, status, xhr) {
// Change the display name and title of the list item.
var changeItem = updateListItem(listItem.d.__metadata);
changeItem.done(function (data, status, xhr) {
alert('file uploaded and updated');
});
changeItem.fail(onError);
});
getItem.fail(onError);
});
addFile.fail(onError);
}
});
// end of the magic !
} catch (e) {
$ul.append("<li class='error'>Error reading " + theFile.name + " : " + e.message + "</li>");
}
$result.append($ul);
}
})(f);
}
}
// Add the file to the file collection in the Shared Documents folder.
function addFileToFolder(arrayBuffer, filename) {
// Get the file name from the file input control on the page.
var nameArray = filename.split('/');
var filenamev = nameArray[nameArray.length - 1];
//filenamev = filenamev.substring(0, filenamev.indexOf("."));
var serverRelativeUrlToFolder = 'Shared Documents';
// Construct the endpoint.
var fileCollectionEndpoint = String.format(
"{0}/_api/sp.appcontextsite(@target)/web/getfolderbyserverrelativeurl('{1}')/files" +
"/add(overwrite=true, url='{2}')?@target='{3}'",
appWebUrl, serverRelativeUrlToFolder, filenamev, hostWebUrl);
// Send the request and return the response.
// This call returns the SharePoint file.
return jQuery.ajax({
url: fileCollectionEndpoint,
type: "POST",
data: arrayBuffer,
processData: false,
headers: {
"accept": "application/json;odata=verbose",
"X-RequestDigest": jQuery("#__REQUESTDIGEST").val(),
"content-length": arrayBuffer.byteLength
}
});
}In Code
<1> Jszip with constructor doesn't accept parameters . Please provide a substitute for it.
<2> asArrayBuffer method is removed from Jszip 3.1.2 . Please provide substitute for it.