Browse Source

add XMLHttpRequest

ChrisFuck 5 years ago
parent
commit
225aff7dd6
1 changed files with 54 additions and 10 deletions
  1. 54
    10
      index.ts

+ 54
- 10
index.ts View File

@@ -14,6 +14,8 @@ interface upload_sdk {
14 14
   onContinue: () => number
15 15
   //错误
16 16
   onError: () => number
17
+  //http 请求
18
+  post: () => Promise<any>
17 19
 
18 20
 }
19 21
 
@@ -23,31 +25,43 @@ class UploadSdk implements upload_sdk {
23 25
   private origin: string
24 26
   private token: string
25 27
   private file: File
26
-  private md5: any
27 28
   private base64: any
28
-  
29
+  private BYTES_PER_CHUNK: number = 1024 * 1024
30
+  private IMAGE_MULTIPART = '/multipart/upload/'
31
+  private VIDEO_MULTIPART = '/upload/'
32
+  private chunkNum: any
33
+  private chunkSize: any
34
+  private fileType: any
35
+  private fileSize: any
36
+  private base64Body: any
37
+  private md5: any
38
+  private offset = 0; //用于断点续传,默认为 0
29 39
 
30 40
   constructor(host: string, origin: string, token: string, file: File){
31 41
     this.host = host
32 42
     this.origin = origin
33 43
     this.token = token
34 44
     this.file = file
45
+    this.fileSize = file.size
46
+    this.fileType = file.type
47
+    this.chunkNum =  this.BYTES_PER_CHUNK > this.fileSize ? Math.ceil(this.fileSize / this.BYTES_PER_CHUNK):1
48
+    this.chunkSize = this.BYTES_PER_CHUNK > this.fileSize ? this.fileSize:this.BYTES_PER_CHUNK
35 49
   }
36 50
 
37
-  generateMd5 = async (file: File) => {
38
-    let md5 = await getFilemd5sum(file)
51
+  generateMd5 = async () => {
52
+    let md5 = await getFilemd5sum(this.file)
39 53
     return md5
40 54
   }
41 55
 
42
-  generateBase64 = async (file: File) => {
43
-    let base64 = await getBase64(file)
56
+  generateBase64 = async () => {
57
+    let base64 = await getBase64(this.file)
44 58
     return base64
45 59
   }
46 60
   //上传
47
-  action = async () => {
48
-    return new Promise((resolve, reject) => {
49
-      resolve()
50
-    })
61
+  action = () => {
62
+    this.md5 = this.generateMd5()
63
+    this.base64Body = this.generateBase64()
64
+    return this.post()
51 65
   }
52 66
   //成功返回函数
53 67
   onSuccess =  () => {
@@ -62,6 +76,34 @@ class UploadSdk implements upload_sdk {
62 76
     return 200
63 77
   }
64 78
 
79
+  post = () => {
80
+    return new Promise((resolve, reject) => {
81
+      let xhr = new XMLHttpRequest()
82
+      xhr.open("POSt", 'http://api.links123.net/mps/v1/'+this.IMAGE_MULTIPART+'forum', true)
83
+        xhr.withCredentials = true;
84
+        xhr.setRequestHeader('Authorization', `Bearer ${this.token}`);
85
+        xhr.setRequestHeader('Content-Type', 'application/json; charset=utf-8');
86
+        xhr.setRequestHeader('Accept', 'application/json');
87
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_SIZE',this.fileSize)
88
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_INDEX',1+'')
89
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_NUM',this.chunkNum)
90
+        xhr.setRequestHeader('LINK_UPLOAD_CHUNK_SIZE',this.chunkSize)
91
+        xhr.setRequestHeader('LINK_UPLOAD_OFFSET',this.offset+'')
92
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_MD5',this.md5)
93
+        xhr.setRequestHeader('LINK_UPLOAD_FILE_TYPE','jepg')
94
+        xhr.send(JSON.stringify({body: this.base64Body}))
95
+        xhr.onreadystatechange = function () {
96
+          if (xhr.readyState === 4) {
97
+            if (xhr.status === 304 || (xhr.status >= 200 && xhr.status < 300)) {
98
+              resolve(xhr.responseText)
99
+            } else {
100
+              reject(xhr.status)
101
+            }
102
+          }
103
+        }
104
+    })
105
+  }
106
+
65 107
 }
66 108
 
67 109
 
@@ -70,3 +112,5 @@ class UploadSdk implements upload_sdk {
70 112
 
71 113
 
72 114
 
115
+
116
+