You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
3.5 KiB
87 lines
3.5 KiB
import { HttpClient } from '@angular/common/http'; |
|
import { Injectable } from '@angular/core'; |
|
import { Observable } from 'rxjs'; |
|
|
|
@Injectable({ |
|
providedIn: 'root' |
|
}) |
|
export class ObjectsService { |
|
|
|
static readonly c_apiRoot = "/api/";//普通上传的API根路径 |
|
static readonly c_apiRoot_Multipart = "/api/";//分块上传的API根路径 |
|
|
|
// static testPath = "f1/f2";//测试路径 |
|
|
|
constructor(private http: HttpClient) { } |
|
|
|
|
|
|
|
//普通上传,单个文件上限5M |
|
static baseUrl = ObjectsService.c_apiRoot + 'Objects/WebPlan/'; |
|
postFile(extensionPath: string, file: File): Observable<Object> { |
|
let formData = new FormData() |
|
//console.log(file.name + file.type); |
|
formData.append("file", file, file.name) |
|
|
|
let data = { keepOriginalName: 'true' } |
|
return this.http.post(ObjectsService.baseUrl + extensionPath, formData, { params: data }); |
|
} |
|
|
|
|
|
|
|
|
|
//分块上传 |
|
static baseUrl_MultipartUpload = ObjectsService.c_apiRoot_Multipart + 'NewMultipartUpload/WebPlan/'; |
|
// { |
|
// "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(ObjectsService.baseUrl + extensionPath, formData); |
|
let data = { keepOriginalName: 'true', filename: file.name } |
|
return new Promise((resolve, reject) => { |
|
this.http.post(ObjectsService.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===共分多少段 |
|
|
|
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(ObjectsService.c_apiRoot_Multipart + `MultipartUpload/WebPlan/${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 = ObjectsService.c_apiRoot_Multipart + 'CompleteMultipartUpload/WebPlan/' + objectName; |
|
this.http.post(path, data, { params: paramsData }).subscribe(data => { |
|
let objData: any = new Object(); |
|
objData.fileName = file.name; |
|
objData.filePath = (ObjectsService.baseUrl + objectName).replace(file.name, ""); |
|
resolve(objData) |
|
}) |
|
} |
|
}//for循环 |
|
|
|
//分块 处理 |
|
}) |
|
}) |
|
} |
|
}
|
|
|