本文整理匯總了TypeScript中@ionic-native/file.File.readAsArrayBuffer方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript File.readAsArrayBuffer方法的具體用法?TypeScript File.readAsArrayBuffer怎麽用?TypeScript File.readAsArrayBuffer使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類@ionic-native/file.File
的用法示例。
在下文中一共展示了File.readAsArrayBuffer方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1:
readFileAsArrayBuffer: (directory: string, name: string) =>
Bluebird.resolve(FILE.readAsArrayBuffer(directory, name)),
示例2: fixFileReader
const readFileAsBlob = (path, filename, type) => {
fixFileReader()
return FILE.readAsArrayBuffer(path, filename).then((buf) => new Blob([buf], { type }))
}
示例3: unzip
/**
* Extracts files from a ZIP archive.
*
* @param {string} source Path to the source ZIP file.
* @param {string} destination Destination folder.
* @param {Function} [onProgress] Optional callback to be called on progress update
* @return {Promise<number>} Promise that resolves with a number. 0 is success, -1 is error.
*/
unzip(source: string, destination: string, onProgress?: Function): Promise<number> {
// Replace all %20 with spaces.
source = source.replace(/%20/g, ' ');
destination = destination.replace(/%20/g, ' ');
const sourceDir = source.substring(0, source.lastIndexOf('/')),
sourceName = source.substr(source.lastIndexOf('/') + 1),
zip = new JSZip();
// Read the file first.
return this.file.readAsArrayBuffer(sourceDir, sourceName).then((data) => {
// Now load the file using the JSZip library.
return zip.loadAsync(data);
}).then((): any => {
if (!zip.files || !Object.keys(zip.files).length) {
// Nothing to extract.
return 0;
}
// First of all, create the directory where the files will be unzipped.
const destParent = destination.substring(0, source.lastIndexOf('/')),
destFolderName = destination.substr(source.lastIndexOf('/') + 1);
return this.file.createDir(destParent, destFolderName, false);
}).then(() => {
const promises = [],
total = Object.keys(zip.files).length;
let loaded = 0;
for (const name in zip.files) {
const file = zip.files[name];
let promise;
if (!file.dir) {
// It's a file.
const fileDir = name.substring(0, name.lastIndexOf('/')),
fileName = name.substr(name.lastIndexOf('/') + 1),
filePromises = [];
let fileData;
if (fileDir) {
// The file is in a subfolder, create it first.
filePromises.push(this.createDir(destination, fileDir));
}
// Read the file contents as a Blob.
filePromises.push(file.async('blob').then((data) => {
fileData = data;
}));
promise = Promise.all(filePromises).then(() => {
// File read and parent folder created, now write the file.
const parentFolder = this.textUtils.concatenatePaths(destination, fileDir);
return this.file.writeFile(parentFolder, fileName, fileData, {replace: true});
});
} else {
// It's a folder, create it if it doesn't exist.
promise = this.createDir(destination, name);
}
promises.push(promise.then(() => {
// File unzipped, call the progress.
loaded++;
onProgress && onProgress({ loaded: loaded, total: total });
}));
}
return Promise.all(promises).then(() => {
return 0;
});
}).catch(() => {
// Error.
return -1;
});
}