MCPcopy Create free account
hub / github.com/ptmt/react-native-macos / download

Function download

Examples/UIExplorer/js/XHRExample.android.js:80–144  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

78 }
79
80 download() {
81 this.xhr && this.xhr.abort();
82
83 var xhr = this.xhr || new XMLHttpRequest();
84 const onreadystatechange = () => {
85 if (xhr.readyState === xhr.HEADERS_RECEIVED) {
86 const contentLength = parseInt(xhr.getResponseHeader('Content-Length'), 10);
87 this.setState({
88 contentLength,
89 responseLength: 0,
90 });
91 } else if (xhr.readyState === xhr.LOADING && xhr.response) {
92 this.setState({
93 responseLength: xhr.response.length,
94 });
95 }
96 };
97 const onprogress = (event) => {
98 this.setState({
99 progressTotal: event.total,
100 progressLoaded: event.loaded,
101 });
102 };
103
104 if (this.state.readystateHandler) {
105 xhr.onreadystatechange = onreadystatechange;
106 }
107 if (this.state.progressHandler) {
108 xhr.onprogress = onprogress;
109 }
110 if (this.state.arraybuffer) {
111 xhr.responseType = 'arraybuffer';
112 }
113 xhr.onload = () => {
114 this.setState({downloading: false});
115 if (this.cancelled) {
116 this.cancelled = false;
117 return;
118 }
119 if (xhr.status === 200) {
120 let responseType = `Response is a string, ${xhr.response.length} characters long.`;
121 if (typeof ArrayBuffer !== 'undefined' &&
122 xhr.response instanceof ArrayBuffer) {
123 responseType = `Response is an ArrayBuffer, ${xhr.response.byteLength} bytes long.`;
124 }
125 this.setState({status: `Download complete! ${responseType}`});
126 } else if (xhr.status !== 0) {
127 this.setState({
128 status: 'Error: Server returned HTTP status of ' + xhr.status + ' ' + xhr.responseText
129 });
130 } else {
131 this.setState({status: 'Error: ' + xhr.responseText});
132 }
133 };
134 xhr.open('GET', 'http://aleph.gutenberg.org/cache/epub/100/pg100.txt.utf8');
135 // Avoid gzip so we can actually show progress
136 xhr.setRequestHeader('Accept-Encoding', '');
137 xhr.send();

Callers

nothing calls this directly

Calls 4

setRequestHeaderMethod · 0.80
abortMethod · 0.45
openMethod · 0.45
sendMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…