-
-
Notifications
You must be signed in to change notification settings - Fork 597
(Browser / React-Native) Support for File Uri Upload #765
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
865410f
00ebf3d
a0fe552
ef6607f
4876c0d
b3b9408
2ad91da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,11 +8,14 @@ | |
* | ||
* @flow | ||
*/ | ||
/* global File */ | ||
/* global XMLHttpRequest, File */ | ||
import CoreManager from './CoreManager'; | ||
import type { FullOptions } from './RESTController'; | ||
const http = require('http'); | ||
const https = require('https'); | ||
|
||
let XHR = null; | ||
if (typeof XMLHttpRequest !== 'undefined') { | ||
XHR = XMLHttpRequest; | ||
} | ||
|
||
type Base64 = { base64: string }; | ||
type FileData = Array<number> | Base64 | File; | ||
|
@@ -311,10 +314,16 @@ const DefaultController = { | |
}, | ||
|
||
download: function(uri) { | ||
if (XHR) { | ||
return this.downloadAjax(uri); | ||
} | ||
if (process.env.PARSE_BUILD !== 'node') { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe i don't understand what process.env.PARSE_BUILD is, but the way I'm reading this is: if we're not in node, complain that we don't have XMLHttpRequest, but XMLHttpRequest is in the browser, not node. set me straight here :). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For browsers that don't support ajax. https://www.tutorialspoint.com/ajax/ajax_browser_support.htm There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. PARSE_BUILD is set when building the sdk. https://github.com/parse-community/Parse-SDK-JS/blob/master/build_releases.sh |
||
return Promise.reject('Cannot make a request: No definition of XMLHttpRequest was found.'); | ||
} | ||
return new Promise((resolve, reject) => { | ||
let client = http; | ||
let client = require('http'); | ||
if (uri.indexOf('https') === 0) { | ||
client = https; | ||
client = require('https'); | ||
} | ||
client.get(uri, (resp) => { | ||
resp.setEncoding('base64'); | ||
|
@@ -328,6 +337,30 @@ const DefaultController = { | |
}); | ||
}).on('error', reject); | ||
}); | ||
}, | ||
|
||
downloadAjax: function(uri) { | ||
return new Promise((resolve, reject) => { | ||
const xhr = new XHR(); | ||
xhr.open('GET', uri, true); | ||
xhr.responseType = 'arraybuffer'; | ||
xhr.onerror = function(e) { reject(e); }; | ||
xhr.onreadystatechange = function() { | ||
if (xhr.readyState !== 4) { | ||
return; | ||
} | ||
const bytes = new Uint8Array(this.response); | ||
resolve({ | ||
base64: ParseFile.encodeBase64(bytes), | ||
contentType: xhr.getResponseHeader('content-type'), | ||
}); | ||
}; | ||
xhr.send(); | ||
}); | ||
}, | ||
|
||
_setXHR(xhr: any) { | ||
XHR = xhr; | ||
} | ||
}; | ||
|
||
|
Uh oh!
There was an error while loading. Please reload this page.