|
@@ -0,0 +1,50 @@
|
|
|
|
|
+import client from '@/utils/ali-oss';
|
|
|
|
|
+
|
|
|
|
|
+// 自定义请求头
|
|
|
|
|
+const headers = {
|
|
|
|
|
+ // 指定Object的存储类型。
|
|
|
|
|
+ 'x-oss-storage-class': 'Standard',
|
|
|
|
|
+ // 指定Object的访问权限。
|
|
|
|
|
+ 'x-oss-object-acl': 'private',
|
|
|
|
|
+ // 通过文件URL访问文件时,指定以附件形式下载文件,下载后的文件名称定义为example.txt。
|
|
|
|
|
+ 'Content-Disposition': 'attachment; filename="example.txt"',
|
|
|
|
|
+ // 设置Object的标签,可同时设置多个标签。
|
|
|
|
|
+ 'x-oss-tagging': 'Tag1=1&Tag2=2',
|
|
|
|
|
+ // 指定PutObject操作时是否覆盖同名目标Object。此处设置为true,表示禁止覆盖同名Object。
|
|
|
|
|
+ 'x-oss-forbid-overwrite': 'true',
|
|
|
|
|
+};
|
|
|
|
|
+
|
|
|
|
|
+async function uploadAliOss(data) {
|
|
|
|
|
+ try {
|
|
|
|
|
+ // 填写OSS文件完整路径和本地文件的完整路径。OSS文件完整路径中不能包含Bucket名称。
|
|
|
|
|
+ // 如果本地文件的完整路径中未指定本地路径,则默认从示例程序所属项目对应本地路径中上传文件。
|
|
|
|
|
+ console.log(blobToFile(base64ToBlob(data), 'h5UploadAliOss'));
|
|
|
|
|
+ const result = await client.put(
|
|
|
|
|
+ 'h5UploadAliOss', //获取一个随机的文件名
|
|
|
|
|
+ blobToFile(base64ToBlob(data), 'h5UploadAliOss') //base64转file对象
|
|
|
|
|
+ // { 'Content-Type': 'image/jpeg' } //设置Content-Type
|
|
|
|
|
+ );
|
|
|
|
|
+ console.log(result);
|
|
|
|
|
+ } catch (e) {
|
|
|
|
|
+ console.log(e);
|
|
|
|
|
+ }
|
|
|
|
|
+}
|
|
|
|
|
+export function base64ToBlob(base64Data) {
|
|
|
|
|
+ let arr = base64Data.split(','),
|
|
|
|
|
+ fileType = arr[0].match(/:(.*?);/)[1],
|
|
|
|
|
+ bstr = atob(arr[1]),
|
|
|
|
|
+ l = bstr.length,
|
|
|
|
|
+ u8Arr = new Uint8Array(l);
|
|
|
|
|
+ while (l--) {
|
|
|
|
|
+ u8Arr[l] = bstr.charCodeAt(l);
|
|
|
|
|
+ }
|
|
|
|
|
+ return new Blob([u8Arr], {
|
|
|
|
|
+ type: fileType,
|
|
|
|
|
+ });
|
|
|
|
|
+}
|
|
|
|
|
+export function blobToFile(newBlob, fileName) {
|
|
|
|
|
+ newBlob.lastModifiedDate = new Date();
|
|
|
|
|
+ newBlob.name = fileName;
|
|
|
|
|
+ return newBlob;
|
|
|
|
|
+}
|
|
|
|
|
+export default uploadAliOss;
|