-
Notifications
You must be signed in to change notification settings - Fork 0
/
httpRequest.js
126 lines (126 loc) · 4.83 KB
/
httpRequest.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
///////////////////header///////////////////
// var header = { Authorization: '' };
///////////////////post////////////////////
// httpRequest.post(url, { }, header).then(res => {
// window.console.log(res)
// }).catch(err => {
// alert(err);
// }).finally(fin => {
// window.console.log(fin);
// });
//////////////////////get////////////////////
// httpRequest.get(url, header).then(res => {
// window.console.log(res)
// }).catch(err => {
// alert(err);
// }).finally(com => {
// window.console.log(com);
// });
/////////////////postFile有进度//////////////
// var file = document.getElementById("file").files[0];
// var formData = new FormData();
// formData.append("files", file);
// formData.append("roles", JSON.stringify(["manage"]));
// httpRequest.postFileProgress(url, formData, header,
// function (data) {
// window.console.log(data);
// },
// function (percent) {
// window.console.log(percent)
// });
/////////////////postFile///////////////////
// var file = document.getElementById("file").files[0];
// var formData = new FormData();
// formData.append("files", file);
// formData.append("roles", JSON.stringify(["manage"]));
// httpRequest.postFile(url, formData, header).then(res => {
// window.console.log(res)
// }).catch(err => {
// alert(err);
// }).finally(fin => {
// window.console.log(fin);
// })
//////////////////downloadFile///////////////
// httpRequest.downloadFile(url, null, header,"1.jpg");
/////////////////////////////////////////////
var httpRequest = {
//fetch不具备查看进度功能
postFile: function (url, formData, headers) {
var options = { method: 'post' };
if (headers == null) headers = {};
var headers_copy = {};
Object.assign(headers_copy, headers);
options.headers = new Headers(headers_copy);
options.body = formData;
return fetch(url, options).then(res => res.json());
},
//上传文件可以查看进度
postFileProgress: function (url, formData, headers, success, onprogress, onerror) {
var xhr = new XMLHttpRequest();
xhr.upload.onprogress = function (event) {
var percent = (event.loaded / event.total * 100).toFixed();
onprogress(percent); //进度
}
xhr.onload = function (event) {
var target = event.srcElement || event.target;
success(JSON.parse(target.responseText));
};
xhr.onerror = onerror;
xhr.open("post", url);
if (headers) {
for (var key in headers) xhr.setRequestHeader(key, headers[key]);
}
xhr.send(formData);
},
post: function (url, jsonData, headers) {
var options = { method: 'post' };
if (headers == null) headers = {};
var headers_copy = {};
Object.assign(headers_copy, headers);
headers_copy["Content-Type"] = "application/json";
options.headers = new Headers(headers_copy);
options.credentials = "same-origin";
if (jsonData != null) options.body = JSON.stringify(jsonData);
return fetch(url, options).then(res => res.json());
},
get: function (url, headers) {
var options = { method: 'get' };
var headers_copy = {};
Object.assign(headers_copy, headers);
if (headers != null) options.headers = new Headers(headers_copy);
return fetch(url, options).then(res => res.json());
},
getFile: function (url, jsonData, headers) {
var options = { method: 'post' };
if (headers == null) headers = {};
var headers_copy = {};
Object.assign(headers_copy, headers);
headers_copy["Content-Type"] = "application/json";
options.headers = new Headers(headers_copy);
options.credentials = "same-origin";
if (jsonData != null) options.body = JSON.stringify(jsonData);
return fetch(url, options);
},
filename: "",
downloadFile: function (url, jsonData, headers, filename) {
this.filename = filename;
this.getFile(url, jsonData, headers).then(res => {
if (!httpRequest.filename) {
var contentDisposition = res.headers.get('Content-Disposition');
if (contentDisposition) httpRequest.filename = contentDisposition.split(';')[1].split('=')[1];
}
res.blob().then(blob => {
var a = document.createElement('a');
var url = window.URL.createObjectURL(blob);
a.href = url;
a.download = httpRequest.filename;
a.click();
window.URL.revokeObjectURL(url);
httpRequest.filename = "";
});
})
}
}
module.exports = {
httpRequest: httpRequest
}