经过两周时间的折腾,终于实现了在Ionic中将图片上传到腾讯云COS中。这里记录一下自己踩过的坑。
在腾讯云COS提供的SDK包中,只有JavaScript-SDK包,该包的实现是采用Jquery实现的。而在我们Ionic开发中是采用Angular实现的,所以不太想引入Jquery的相关内容。所以想参考他的JavaScript-SDK的源码自己实现图片上传功能。
在JavaScript-SDK中文件的上传代码如下:
CosCloud.prototype.uploadFile = function (success, error, onprogress, bucketName, remotePath, file, insertOnly, taskReady) {var that = this;if (file.size > MAX_UNSLICE_FILE_SIZE) {that.sliceUploadFile(success, error, onprogress, bucketName, remotePath, file, insertOnly, undefined, undefined, taskReady);return;}if (remotePath.substr(remotePath.length - 1) === /) {error({code: -1, message: path not allow end with "/"});return;}remotePath = fixPath(remotePath);// 辅助 cancelTaskvar taskId = guid();var $xhr;var globalTask = {id: taskId,state: uploading,cancel: function () {$xhr && $xhr.abort();}};this.tasks[taskId] =