|
@@ -2,6 +2,7 @@ const AliOss = (function (){
|
|
|
const setting = {
|
|
|
hint: true
|
|
|
};
|
|
|
+ let client, uploadInfo;
|
|
|
|
|
|
const downloadFile = function (url, filename) {
|
|
|
axios.get(url, {responseType: 'blob' }).then(res => {
|
|
@@ -54,5 +55,121 @@ const AliOss = (function (){
|
|
|
setting.hint = data.hint || setting.hint;
|
|
|
};
|
|
|
|
|
|
- return { downloadFile, downloadFileSync, zipFiles, setSetting }
|
|
|
+ const stopUpload = async function() {
|
|
|
+ if (!client) toastr.warning('当前没有上传中的大文件');
|
|
|
+
|
|
|
+ client.cancel();
|
|
|
+ uploadInfo.isStop = true;
|
|
|
+ if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');
|
|
|
+ };
|
|
|
+
|
|
|
+ const resumeUpload = async function() {
|
|
|
+ if (!client || !uploadInfo.abortCheckpoint) return;
|
|
|
+
|
|
|
+ if (uploadInfo.stopObj) uploadInfo.stopObj.removeAttr('disabled');
|
|
|
+ if (uploadInfo.resumeObj) uploadInfo.resumeObj.attr('disabled', 'disabled');
|
|
|
+ try {
|
|
|
+ const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {
|
|
|
+ checkpoint: uploadInfo.abortCheckpoint,
|
|
|
+ progress: (p, cpt, res) => {
|
|
|
+ uploadInfo.abortCheckpoint = cpt;
|
|
|
+ // 刷新进度条
|
|
|
+ if (uploadInfo.progressObj) {
|
|
|
+ uploadInfo.progressObj.attr('aria-valuenow', p * 100);
|
|
|
+ uploadInfo.progressObj.width((p * 100) + '%');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return await endUploadBigFile();
|
|
|
+ } catch (e) {
|
|
|
+ if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const resetRelaObj = function() {
|
|
|
+ if (uploadInfo.progressObj) {
|
|
|
+ uploadInfo.progressObj.attr('aria-valuenow', 0);
|
|
|
+ uploadInfo.progressObj.width('0%');
|
|
|
+ }
|
|
|
+ if (uploadInfo.stopObj) {
|
|
|
+ uploadInfo.stopObj.show();
|
|
|
+ uploadInfo.stopObj.removeAttr('disabled');
|
|
|
+ uploadInfo.stopObj.bind('click', function() {
|
|
|
+ stopUpload();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ if (uploadInfo.resumeObj) {
|
|
|
+ uploadInfo.resumeObj.show();
|
|
|
+ uploadInfo.resumeObj.attr('disabled', 'disabled');
|
|
|
+ uploadInfo.resumeObj.bind('click', function() {
|
|
|
+ resumeUpload();
|
|
|
+ });
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const clearRelaObj = function() {
|
|
|
+ if (uploadInfo.stopObj) {
|
|
|
+ uploadInfo.stopObj.attr('disabled', 'disabled');
|
|
|
+ uploadInfo.stopObj.unbind('click');
|
|
|
+ }
|
|
|
+ if (uploadInfo.resumeObj) {
|
|
|
+ uploadInfo.resumeObj.attr('disabled', 'disabled');
|
|
|
+ uploadInfo.resumeObj.unbind('click');
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ const beginUploadBigFile = async function () {
|
|
|
+ const beingData = JSON.parse(JSON.stringify(uploadInfo.defaultData));
|
|
|
+ beingData.type = 'begin';
|
|
|
+ beingData.fileInfo = uploadInfo.fileInfo;
|
|
|
+ const info = await postDataAsync(uploadInfo.url, beingData, false);
|
|
|
+ uploadInfo.filepath = info.filepath;
|
|
|
+ uploadInfo.filename = info.filename;
|
|
|
+ if (!info.oss) throw '授权信息错误';
|
|
|
+ uploadInfo.oss = info.oss;
|
|
|
+ };
|
|
|
+ const endUploadBigFile = async function() {
|
|
|
+ client = null;
|
|
|
+ const endData = JSON.parse(JSON.stringify(uploadInfo.defaultData));
|
|
|
+ endData.type = 'end';
|
|
|
+ endData.filepath = uploadInfo.filename;
|
|
|
+ endData.fileInfo = uploadInfo.fileInfo;
|
|
|
+ clearRelaObj();
|
|
|
+ const uploadRes = await postDataAsync(uploadInfo.url, endData, false);
|
|
|
+ if (uploadInfo.callback) {
|
|
|
+ uploadInfo.callback(uploadRes);
|
|
|
+ }
|
|
|
+ uploadInfo = {};
|
|
|
+ return uploadRes;
|
|
|
+ };
|
|
|
+
|
|
|
+ const uploadBigFile = async function (file, url, data, relaObj, callback) {
|
|
|
+ uploadInfo = { url, file, ...relaObj, callback };
|
|
|
+ uploadInfo.fileInfo = { filename: file.name, filesize: file.size, type: file.type, lastModified: file.lastModified };
|
|
|
+ uploadInfo.defaultData = data;
|
|
|
+ await beginUploadBigFile();
|
|
|
+
|
|
|
+ try {
|
|
|
+ client = new OSS(uploadInfo.oss);
|
|
|
+ resetRelaObj();
|
|
|
+ const result = await client.multipartUpload(uploadInfo.filepath, uploadInfo.file, {
|
|
|
+ progress: (p, cpt, res) => {
|
|
|
+ uploadInfo.abortCheckpoint = cpt;
|
|
|
+ // 刷新进度条
|
|
|
+ if (uploadInfo.progressObj) {
|
|
|
+ uploadInfo.progressObj.attr('aria-valuenow', p * 100);
|
|
|
+ uploadInfo.progressObj.width((p * 100) + '%');
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+
|
|
|
+ return await endUploadBigFile();
|
|
|
+ } catch (err) {
|
|
|
+ if (uploadInfo.resumeObj) uploadInfo.resumeObj.removeAttr('disabled');
|
|
|
+ if (uploadInfo.stopObj) uploadInfo.stopObj.attr('disabled', 'disabled');
|
|
|
+ }
|
|
|
+ };
|
|
|
+
|
|
|
+ return { downloadFile, downloadFileSync, zipFiles, setSetting, uploadBigFile, resumeUpload }
|
|
|
})();
|