aliyun-oss-react-native

UploadManager.js 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. import React, { Component } from 'react';
  2. import {
  3. Platform,
  4. StyleSheet,
  5. Text,
  6. View,
  7. Alert,
  8. Button,
  9. ScrollView,
  10. Image,
  11. DeviceEventEmitter
  12. } from 'react-native';
  13. //保存初始化分片上传ID
  14. let uploadId;
  15. const multipartBucket = "luozhang002"
  16. const mulitipartObject = "extremlyboy"
  17. let ImagePicker = require('react-native-image-picker');
  18. //导入样式
  19. import { styles } from '../CSS/global.js'
  20. const options = {
  21. title: 'Select Avatar',
  22. customButtons: [
  23. {name: 'fb', title: 'Choose Photo from Facebook'},
  24. ],
  25. storageOptions: {
  26. skipBackup: true,
  27. path: 'images'
  28. }
  29. };
  30. export class UploadManager extends Component {
  31. render() {
  32. return(
  33. <View style={styles.item}>
  34. <Text style={styles.description}>文件上传操作</Text>
  35. <Image source={require('../resource/putao.jpeg')}/>
  36. <View style={styles.detailitem}>
  37. <View style={styles.button}>
  38. <Button
  39. onPress={this.handleClick.bind(this,"uploadFile")}
  40. title="上传文件"
  41. color="#841584"
  42. />
  43. </View>
  44. <View style={styles.button}>
  45. <Button style={styles.button}
  46. onPress={this.handleClick.bind(this,"appendObject")}
  47. title="追加文件"
  48. color="#841584"
  49. />
  50. </View>
  51. <View style={styles.button}>
  52. <Button style={styles.button}
  53. onPress={this.handleClick.bind(this,"resumeObject")}
  54. title="断点续传"
  55. color="#841584"
  56. />
  57. </View>
  58. <View style={styles.button}>
  59. <Button style={styles.button}
  60. onPress={this.handleClick.bind(this,"initMultipartUpload")}
  61. title="初始化分片"
  62. color="#841584"
  63. />
  64. </View>
  65. <View style={styles.button}>
  66. <Button style={styles.button}
  67. onPress={this.handleClick.bind(this,"multipartUpload")}
  68. title="分片上传"
  69. color="#841584"
  70. />
  71. </View>
  72. <View style={styles.button}>
  73. <Button style={styles.button}
  74. onPress={this.handleClick.bind(this,"abortMultipartUpload")}
  75. title="取消分片上传"
  76. color="#841584"
  77. />
  78. </View>
  79. <View style={styles.button}>
  80. <Button style={styles.button}
  81. onPress={this.handleClick.bind(this,"listParts")}
  82. title="列出分片"
  83. color="#841584"
  84. />
  85. </View>
  86. </View>
  87. </View>
  88. )
  89. }
  90. handleClick(e) {
  91. switch (e) {
  92. case 'uploadFile' : {
  93. console.log("准备上传")
  94. ImagePicker.showImagePicker(options, (response) => {
  95. console.log('Response = ', response);
  96. if (response.didCancel) {
  97. console.log('User cancelled image picker');
  98. }
  99. else if (response.error) {
  100. console.log('ImagePicker Error: ', response.error);
  101. }
  102. else if (response.customButton) {
  103. console.log('User tapped custom button: ', response.customButton);
  104. }
  105. else {
  106. let source = { uri: response.uri };
  107. AliyunOSS.asyncUpload("luozhang002", "xxxxxxxx/yanxing", source.uri).then((res)=>{
  108. Alert.alert(
  109. 'Alert Title',
  110. "恭喜你成功上传到阿里云服务器",
  111. [
  112. {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
  113. {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
  114. {text: 'OK', onPress: () => console.log('OK Pressed')},
  115. ],
  116. { cancelable: false }
  117. )
  118. })
  119. }
  120. });
  121. } break;
  122. case "appendObject" : {
  123. ImagePicker.showImagePicker(options, (response) => {
  124. console.log('Response = ', response);
  125. if (response.didCancel) {
  126. console.log('User cancelled image picker');
  127. }
  128. else if (response.error) {
  129. console.log('ImagePicker Error: ', response.error);
  130. }
  131. else if (response.customButton) {
  132. console.log('User tapped custom button: ', response.customButton);
  133. }
  134. else {
  135. let source = { uri: response.uri };
  136. let position = 0;
  137. AliyunOSS.asyncAppendObject("luozhang002", "xxx", source.uri,{appendPostion:`${position}`}).then((res)=>{
  138. nextPositon = res.nextPositon;
  139. //再次调用即可
  140. })
  141. }
  142. });
  143. } break;
  144. case "resumeObject" : {
  145. ImagePicker.showImagePicker(options, (response) => {
  146. console.log('Response = ', response);
  147. if (response.didCancel) {
  148. console.log('User cancelled image picker');
  149. }
  150. else if (response.error) {
  151. console.log('ImagePicker Error: ', response.error);
  152. }
  153. else if (response.customButton) {
  154. console.log('User tapped custom button: ', response.customButton);
  155. }
  156. else {
  157. let source = { uri: response.uri };
  158. AliyunOSS.asyncResumableUpload("luozhang002", "zhongji", source.uri).then((res)=>{
  159. Alert.alert(
  160. 'Alert Title',
  161. "恭喜你上传成功",
  162. [
  163. {text: 'Ask me later', onPress: () => console.log('Ask me later pressed')},
  164. {text: 'Cancel', onPress: () => console.log('Cancel Pressed'), style: 'cancel'},
  165. {text: 'OK', onPress: () => console.log('OK Pressed')},
  166. ],
  167. { cancelable: false }
  168. )
  169. })
  170. }
  171. });
  172. } break;
  173. case "initMultipartUpload" : {
  174. AliyunOSS.initMultipartUpload(multipartBucket,multipartBucket).then((e)=>{
  175. Alert.alert("分片初始化成功:" + e);
  176. uploadId = e;
  177. }).catch((error)=>{
  178. console.log(error)
  179. })
  180. } break
  181. case "multipartUpload" : {
  182. ImagePicker.showImagePicker(options, (response) => {
  183. console.log('Response = ', response);
  184. if (response.didCancel) {
  185. console.log('User cancelled image picker');
  186. } else if (response.error) {
  187. console.log('ImagePicker Error: ', response.error);
  188. } else if (response.customButton) {
  189. console.log('User tapped custom button: ', response.customButton);
  190. } else {
  191. let source = { uri: response.uri };
  192. AliyunOSS.multipartUpload(multipartBucket,mulitipartObject,uploadId,source.uri).then((res)=>{
  193. Alert.alert("分片上传成功");
  194. }).catch((e)=>{
  195. Alert.alert("分片上传失败");
  196. })
  197. }
  198. });
  199. } break;
  200. case "abortMultipartUpload" : {
  201. AliyunOSS.abortMultipartUpload(multipartBucket,multipartBucket,uploadId).then((e)=>{
  202. Alert.alert("分片终止成功");
  203. }).catch((e)=>{
  204. Alert.alert("分片终止失败");
  205. })
  206. } break;
  207. case "listParts" : {
  208. AliyunOSS.listParts(multipartBucket,multipartBucket,uploadId).then((e)=>{
  209. Alert.alert("onListParts"+e)
  210. }).catch((e)=>{
  211. Alert.alert("onListPartsError")
  212. })
  213. } break;
  214. default : break;
  215. }
  216. }
  217. componentDidMount() {
  218. const uploadProgress = p => console.log(p.currentSize / p.totalSize);
  219. AliyunOSS.addEventListener('uploadProgress', uploadProgress);
  220. }
  221. }