App.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* eslint-disable */
  2. import React from 'react'
  3. import {
  4. StyleSheet,
  5. Text,
  6. View,
  7. Button,
  8. TouchableOpacity,
  9. Image } from 'react-native'
  10. import Uppy from '@uppy/core'
  11. import XHRUpload from '@uppy/xhr-upload'
  12. import { ImagePicker, Permissions } from 'expo'
  13. // import ImagePicker from 'react-native-image-picker'
  14. // import Tus from '@uppy/tus'
  15. function urlToBlob (url) {
  16. return new Promise((resolve, reject) => {
  17. var xhr = new XMLHttpRequest()
  18. xhr.onerror = reject
  19. xhr.onreadystatechange = () => {
  20. if (xhr.readyState === 4) {
  21. resolve(xhr.response)
  22. }
  23. };
  24. xhr.open('GET', url)
  25. xhr.responseType = 'blob' // convert type
  26. xhr.send()
  27. })
  28. }
  29. // var blob = new Blob(
  30. // ['data:image/svg+xml;base64,PHN2ZyB2aWV3Qm94PSIwIDAgMTIwIDEyMCI+CiAgPGNpcmNsZSBjeD0iNjAiIGN5PSI2MCIgcj0iNTAiLz4KPC9zdmc+Cg=='],
  31. // { type: 'image/svg+xml' }
  32. // )
  33. export default class App extends React.Component {
  34. constructor () {
  35. super()
  36. this.state = {
  37. progress: 0,
  38. total: 0,
  39. file: null
  40. }
  41. this.startUpload = this.startUpload.bind(this)
  42. this.selectPhotoTapped = this.selectPhotoTapped.bind(this)
  43. this.addFileToUppy = this.addFileToUppy.bind(this)
  44. }
  45. componentDidMount () {
  46. this.uppy = Uppy({ autoProceed: false, debug: true })
  47. this.uppy.use(XHRUpload, {
  48. endpoint: 'http://b519d44f.ngrok.io/upload.php',
  49. fieldName: 'my_file'
  50. // getResponseData: (responseText, response) => {
  51. // console.log(responseText)
  52. // }
  53. })
  54. // this.uppy.use(Tus, { endpoint: 'https://master.tus.io/files/' })
  55. this.uppy.on('upload-progress', (file, progress) => {
  56. this.setState({
  57. progress: progress.bytesUploaded,
  58. total: progress.bytesTotal
  59. })
  60. })
  61. this.uppy.on('complete', (ev) => {
  62. this.setState({
  63. status: 'Upload complete ✅'
  64. })
  65. console.log('tadada!')
  66. console.log(this.uppy.state.files)
  67. })
  68. // this.uppy.addFile({
  69. // source: 'ReactNative',
  70. // name: 'test-file.svg',
  71. // type: blob.type,
  72. // data: blob
  73. // })
  74. }
  75. addFileToUppy (file) {
  76. console.log(file)
  77. urlToBlob(file.base64)
  78. .then(blob => {
  79. console.log(blob)
  80. this.uppy.addFile({
  81. source: 'React Native',
  82. name: 'fileName.jpg',
  83. type: blob.type,
  84. data: blob
  85. })
  86. })
  87. // console.log('there is a file:', this.uppy.state.files)
  88. }
  89. selectPhotoTapped () {
  90. console.log('SELECT PHOTO')
  91. Permissions.askAsync(Permissions.CAMERA_ROLL).then((isAllowed) => {
  92. if (!isAllowed) return
  93. ImagePicker.launchImageLibraryAsync({
  94. // allowsEditing: true,
  95. base64: true
  96. })
  97. .then((result) => {
  98. console.log(result)
  99. if (!result.cancelled) {
  100. this.setState({ file: result })
  101. this.addFileToUppy(result)
  102. }
  103. })
  104. })
  105. // ImagePicker.showImagePicker(options, (response) => {
  106. // console.log('Response = ', response);
  107. // if (response.didCancel) {
  108. // console.log('User cancelled photo picker');
  109. // }
  110. // else if (response.error) {
  111. // console.log('ImagePicker Error: ', response.error);
  112. // }
  113. // else if (response.customButton) {
  114. // console.log('User tapped custom button: ', response.customButton);
  115. // }
  116. // else {
  117. // let source = { uri: response.uri };
  118. // // You can also display the image using data:
  119. // // let source = { uri: 'data:image/jpeg;base64,' + response.data };
  120. // this.setState({
  121. // ImageSource: source
  122. // });
  123. // }
  124. // })
  125. }
  126. startUpload () {
  127. this.setState({
  128. status: 'Uploading...'
  129. })
  130. this.uppy.upload()
  131. }
  132. render () {
  133. return (
  134. <View style={styles.container}>
  135. <Text>Uppy running in React Native</Text>
  136. <TouchableOpacity onPress={this.selectPhotoTapped}>
  137. { this.state.file === null
  138. ? <Text>Select a Photo</Text>
  139. : <Image
  140. style={{ width: 200, height: 200 }}
  141. source={{ uri: this.state.file.uri }}
  142. />
  143. }
  144. </TouchableOpacity>
  145. <Text>Status: {this.state.status}</Text>
  146. <Text>{this.state.progress} of {this.state.total}</Text>
  147. <Button
  148. onPress={this.startUpload}
  149. title="Start Upload"
  150. color="#841584"
  151. accessibilityLabel="Start uploading a file"
  152. />
  153. </View>
  154. )
  155. }
  156. }
  157. const styles = StyleSheet.create({
  158. container: {
  159. flex: 1,
  160. backgroundColor: '#fff',
  161. alignItems: 'center',
  162. justifyContent: 'center'
  163. }
  164. })