邵佳豪
2 years ago
4 changed files with 227 additions and 142 deletions
@ -1,86 +1,125 @@
|
||||
import { HttpClient } from '@angular/common/http'; |
||||
import { Injectable } from '@angular/core'; |
||||
import { Observable } from 'rxjs'; |
||||
import { HttpClient } from "@angular/common/http"; |
||||
import { Injectable } from "@angular/core"; |
||||
import { Observable } from "rxjs"; |
||||
|
||||
@Injectable({ |
||||
providedIn: 'root' |
||||
providedIn: "root", |
||||
}) |
||||
export class ObjectsSimpleService { |
||||
static readonly c_apiRoot = "/api/"; //普通上传的API根路径
|
||||
static readonly c_apiRoot_Multipart = "/api/"; //分块上传的API根路径
|
||||
constructor(private http: HttpClient) {} |
||||
|
||||
static readonly c_apiRoot = "/api/";//普通上传的API根路径
|
||||
static readonly c_apiRoot_Multipart = "/api/";//分块上传的API根路径
|
||||
|
||||
// static testPath = "f1/f2";//测试路径
|
||||
|
||||
constructor(private http: HttpClient) { } |
||||
|
||||
|
||||
|
||||
//普通上传,单个文件上限5M
|
||||
static baseUrl = ObjectsSimpleService.c_apiRoot + 'Objects/sinochemweb/'; |
||||
postFile(extensionPath: string, file: File): Observable<Object> { |
||||
let formData = new FormData() |
||||
formData.append("file", file, file.name) |
||||
let data = { keepOriginalName: 'true' } |
||||
return this.http.post(ObjectsSimpleService.baseUrl + extensionPath, formData, { params: data }); |
||||
static getBucketName() { |
||||
let bucket = ""; |
||||
if ( |
||||
window.location.host.indexOf("sinochemoilmarketing") != -1 || |
||||
window.location.host.indexOf("121.36.37.70") != -1 |
||||
) { |
||||
bucket = "/sinochemweb/"; |
||||
} else if (window.location.host.indexOf("anxincloud") != -1) { |
||||
bucket = "/gas/"; |
||||
} else { |
||||
bucket = "/sinochemweb/"; |
||||
} |
||||
console.log("Objects桶名", bucket); |
||||
return bucket; |
||||
} |
||||
|
||||
//普通上传,单个文件上限5M
|
||||
static baseUrl = |
||||
ObjectsSimpleService.c_apiRoot + |
||||
"Objects" + |
||||
ObjectsSimpleService.getBucketName(); |
||||
postFile(extensionPath: string, file: File): Observable<Object> { |
||||
let formData = new FormData(); |
||||
formData.append("file", file, file.name); |
||||
let data = { keepOriginalName: "true" }; |
||||
return this.http.post( |
||||
ObjectsSimpleService.baseUrl + extensionPath, |
||||
formData, |
||||
{ params: data } |
||||
); |
||||
} |
||||
|
||||
//分块上传
|
||||
static baseUrl_MultipartUpload = |
||||
ObjectsSimpleService.c_apiRoot_Multipart + |
||||
"NewMultipartUpload" + |
||||
ObjectsSimpleService.getBucketName(); |
||||
postFile_MultipartUpload(extensionPath: string, file: File): Promise<Object> { |
||||
// let formData = new FormData()
|
||||
// formData.append("file", file, file.name)
|
||||
// return this.http.post(ObjectsSimpleService.baseUrl + extensionPath, formData);
|
||||
let data = { keepOriginalName: "true", filename: file.name }; |
||||
return new Promise((resolve, reject) => { |
||||
this.http |
||||
.post( |
||||
ObjectsSimpleService.baseUrl_MultipartUpload + extensionPath, |
||||
{}, |
||||
{ params: data } |
||||
) |
||||
.subscribe(async (data: any) => { |
||||
//初始化分段上传
|
||||
let objectName = data.objectName; |
||||
let uploadId = data.uploadId; |
||||
let PartNumberETag = []; //每次返回需要保存的信息
|
||||
//分块 处理
|
||||
let fileSize = file.size || null; //上传文件的总大小
|
||||
let shardSize = 5 * 1024 * 1024; //5MB一个分片
|
||||
let allSlice = Math.ceil(fileSize / shardSize); //总文件/5MB===共分多少段
|
||||
console.log("共分多少段" + allSlice); |
||||
for (let i = 0; i < allSlice; i++) { |
||||
//循环分段上传
|
||||
let start = i * shardSize; //切割文件开始位置
|
||||
let end = Math.min(fileSize, start + shardSize); //切割文件结束位置
|
||||
let formData = new FormData(); |
||||
formData.append("file", file.slice(start, end)); |
||||
|
||||
//同步写法实现异步调用
|
||||
let result = await new Promise((resolve, reject) => { |
||||
// await 需要后面返回一个 promise 对象
|
||||
this.http |
||||
.post( |
||||
ObjectsSimpleService.c_apiRoot_Multipart + |
||||
`MultipartUpload` + |
||||
+ObjectsSimpleService.getBucketName() + |
||||
`${objectName}?uploadId=${uploadId}&partNumber=${i + 1}`, |
||||
formData |
||||
) |
||||
.subscribe((data: any) => { |
||||
let msg = { |
||||
partNumber: data.partNumber || null, |
||||
eTag: data.eTag || null, |
||||
}; |
||||
resolve(msg); // 调用 promise 内置方法处理成功
|
||||
}); |
||||
}); |
||||
PartNumberETag.push(result); |
||||
|
||||
if (PartNumberETag.length === allSlice) { |
||||
//分块上传完成
|
||||
|
||||
//分块上传
|
||||
static baseUrl_MultipartUpload = ObjectsSimpleService.c_apiRoot_Multipart + 'NewMultipartUpload/sinochemweb/'; |
||||
// {
|
||||
// "objectName": "string",
|
||||
// "uploadId": "string"
|
||||
// }
|
||||
postFile_MultipartUpload(extensionPath: string, file: File): Promise<Object> { |
||||
// let formData = new FormData()
|
||||
// formData.append("file", file, file.name)
|
||||
// return this.http.post(ObjectsSimpleService.baseUrl + extensionPath, formData);
|
||||
let data = { keepOriginalName: 'true', filename: file.name } |
||||
return new Promise((resolve, reject) => { |
||||
this.http.post(ObjectsSimpleService.baseUrl_MultipartUpload + extensionPath, {}, { params: data }).subscribe(async (data: any) => { //初始化分段上传
|
||||
let objectName = data.objectName |
||||
let uploadId = data.uploadId |
||||
let PartNumberETag = []; //每次返回需要保存的信息
|
||||
//分块 处理
|
||||
let fileSize = file.size || null //上传文件的总大小
|
||||
let shardSize = 5 * 1024 * 1024 //5MB一个分片
|
||||
let allSlice = Math.ceil(fileSize / shardSize) //总文件/5MB===共分多少段
|
||||
console.log('共分多少段' + allSlice) |
||||
for (let i = 0; i < allSlice; i++) { //循环分段上传
|
||||
let start = i * shardSize //切割文件开始位置
|
||||
let end = Math.min(fileSize, start + shardSize); //切割文件结束位置
|
||||
let formData = new FormData() |
||||
formData.append("file", file.slice(start, end)) |
||||
let data = PartNumberETag; |
||||
let paramsData = { uploadId: uploadId }; |
||||
let path = |
||||
ObjectsSimpleService.c_apiRoot_Multipart + |
||||
"CompleteMultipartUpload" + |
||||
ObjectsSimpleService.getBucketName() + |
||||
objectName; |
||||
this.http |
||||
.post(path, data, { params: paramsData }) |
||||
.subscribe((data) => { |
||||
let objData: any = new Object(); |
||||
objData.fileName = file.name; |
||||
objData.filePath = ObjectsSimpleService.baseUrl + objectName; |
||||
resolve(objData); |
||||
}); |
||||
} |
||||
} //for循环
|
||||
|
||||
//同步写法实现异步调用
|
||||
let result = await new Promise((resolve, reject) => { |
||||
// await 需要后面返回一个 promise 对象
|
||||
this.http.post(ObjectsSimpleService.c_apiRoot_Multipart + `MultipartUpload/sinochemweb/${objectName}?uploadId=${uploadId}&partNumber=${i + 1}`, formData).subscribe((data: any) => { |
||||
let msg = { "partNumber": data.partNumber || null, "eTag": data.eTag || null } |
||||
resolve(msg) // 调用 promise 内置方法处理成功
|
||||
}) |
||||
}); |
||||
PartNumberETag.push(result) |
||||
|
||||
if (PartNumberETag.length === allSlice) { //分块上传完成
|
||||
|
||||
let data = PartNumberETag |
||||
let paramsData = { uploadId: uploadId }; |
||||
let path = ObjectsSimpleService.c_apiRoot_Multipart + 'CompleteMultipartUpload/sinochemweb/' + objectName; |
||||
this.http.post(path, data, { params: paramsData }).subscribe(data => { |
||||
let objData: any = new Object(); |
||||
objData.fileName = file.name; |
||||
objData.filePath = ObjectsSimpleService.baseUrl + objectName |
||||
resolve(objData) |
||||
}) |
||||
} |
||||
}//for循环
|
||||
|
||||
//分块 处理
|
||||
}) |
||||
}) |
||||
} |
||||
//分块 处理
|
||||
}); |
||||
}); |
||||
} |
||||
} |
||||
|
Loading…
Reference in new issue