Guest User

Untitled

a guest
Nov 12th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.71 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import {View,Text,StyleSheet,TextInput,TouchableOpacity,Image} from 'react-native';
  3.  
  4. import ImagePicker from 'react-native-image-picker';
  5. import { Dropdown } from 'react-native-material-dropdown';
  6.  
  7. const options={
  8. title:'select a photo',
  9. takePhotoButtonTitle:'Take a Photo',
  10. chooseFrmoLibraryButtonTitle:'Choose from Gallery',
  11. quality:1
  12. };
  13.  
  14.  
  15. class InputUsers extends Component{
  16.  
  17. //constructor have a state that conatains the properties that will recieve the values from Text Inputes
  18. constructor(props){
  19. super(props)
  20. this.state = {
  21. TextInputName:'',
  22.  
  23. iamgeSource: null,
  24. data: [
  25. {
  26. value: 'Computadoras encendidas',
  27. },
  28. {
  29. value: 'Puerta Abierta',
  30. },
  31. {
  32. value: 'llaves perdidas',
  33. },
  34. {
  35. value: 'Retraso de personal',
  36. },
  37. {
  38. value: 'alarmas desactivadas',
  39. },
  40. {
  41. value: 'personal no reconocido',
  42. },
  43. {
  44. value: 'Agresion',
  45. },
  46. {
  47. value: 'Aulas desordenadas',
  48. },
  49. {
  50. value: 'No hay empleados',
  51. },
  52. {
  53. value: 'No llego compa;ero',
  54. },
  55. {
  56. value: 'Fugas de gas',
  57. },
  58. {
  59. value: 'Puerta esta cerrada',
  60. },
  61. ],
  62. }
  63. }
  64.  
  65. selectPhoto(){
  66. ImagePicker.launchCamera(options, (response) => {
  67. console.log('Response = ', response);
  68.  
  69. if (response.didCancel) {
  70. console.log('User cancelled image picker');
  71. }
  72. else if (response.error) {
  73. console.log('ImagePicker Error: ', response.error);
  74. }
  75. else {
  76. let source = { uri: response.uri };
  77. this.setState({
  78. iamgeSource: source
  79. });
  80. }
  81. });
  82. }
  83.  
  84. //arrow function that will fire when press on save button to save data in database via API
  85. InsertUser = ()=>{
  86. //constant varaibles that equal propertes in state
  87. const {TextInputName} = this.state;
  88.  
  89. const {iamgeSource} = this.state;
  90.  
  91. const formData = new FormData();
  92. //Add your input data
  93. formData.append('image_text', TextInputName);
  94.  
  95.  
  96. //Add your photo
  97. //this, retrive the file extension of your photo
  98. const uriPart = iamgeSource.uri.split('.');
  99. const fileExtension = uriPart[uriPart.length - 1];
  100.  
  101. formData.append('image', {
  102. uri: iamgeSource.uri,
  103. name: 'iamgeSource',
  104. type: 'image/jpeg',
  105. });
  106.  
  107. //API that use fetch to input data to database via backend php script
  108. fetch('http://192.168.0.4/conex/subirimagen.php',{
  109. method: 'POST',
  110. headers: {
  111. 'Accept': 'application/json',
  112. 'Content-Type': 'multipart/form-data',
  113. },
  114. body: formData
  115. })
  116. .then((response) => response.json())
  117. .then((responseJson) => {
  118. // return responseJson
  119. alert(responseJson);
  120. this.props.navigation.navigate('home');
  121. })
  122. .catch((error) => {
  123. console.error(error);
  124. });
  125.  
  126. //alert('Pressed!!');
  127. }
  128.  
  129.  
  130. render(){
  131. return(
  132. <View style ={styles.container}>
  133. <TextInput
  134. // value = {this.TextInputName}
  135. placeholder = 'Descripcion del incidente'
  136. onChangeText = {TextInputValue=>this.setState({TextInputName:TextInputValue}) }
  137. underlineColorAndroid = 'transparent'
  138. style = {styles.TextInputStyle}
  139. />
  140.  
  141. <Image style={styles.image}
  142. source={this.state.iamgeSource != null ? this.state.iamgeSource : require('./image/blogSix.png')}
  143. />
  144.  
  145. <TouchableOpacity style = {styles.TouchableOpacityStyle} onPress={this.selectPhoto.bind(this)}>
  146. <Text style = {styles.TextStyle}>Seleccione la foto</Text>
  147. </TouchableOpacity>
  148.  
  149. <TouchableOpacity activeOpacity = {.4} style = {styles.TouchableOpacityStyle} onPress={this.InsertUser}>
  150. <Text style = {styles.TextStyle}>Enviar incidente</Text>
  151. </TouchableOpacity>
  152.  
  153.  
  154.  
  155. </View>
  156. )
  157. }
  158. }
  159.  
  160. const styles = StyleSheet.create ({
  161. container : {
  162. alignItems:'center',
  163. flex:1,
  164. marginTop:5,
  165. backgroundColor:'#fff'
  166. },
  167.  
  168. TextInputStyle :{
  169. textAlign:'center',
  170. marginBottom:7,
  171. width:'90%',
  172. height:40,
  173. borderWidth:1,
  174. borderRadius:5,
  175. borderColor:'#FF5722'
  176. },
  177.  
  178. TextInputStyle2 :{
  179. textAlign:'center',
  180. marginBottom:7,
  181. marginTop:20,
  182. width:'90%',
  183. height:40,
  184. borderWidth:1,
  185. borderRadius:5,
  186. borderColor:'#FF5722'
  187. },
  188.  
  189. TextStyle : {
  190. color:'#fff',
  191. textAlign:'center'
  192. },
  193.  
  194. TouchableOpacityStyle:{
  195. paddingTop:10,
  196. paddingBottom:10,
  197. marginTop:20,
  198. borderRadius:5,
  199. marginBottom:7,
  200. width:'90%',
  201. backgroundColor:'#00BCD4'
  202. },
  203.  
  204. button:{
  205. width:250,
  206. height:50,
  207. backgroundColor:"#330066"
  208. },
  209.  
  210. text:{
  211. color:'white',
  212. fontSize:30,
  213. textAlign:'center'
  214. },
  215.  
  216. textInput2: {
  217. textAlign:'center',
  218. marginBottom:7,
  219. marginTop:20,
  220. width:'90%',
  221. height:40,
  222. borderWidth:1,
  223. borderRadius:5,
  224. borderColor:'#FF5722',
  225. paddingBottom:25,
  226.  
  227. },
  228.  
  229. image:{
  230. width:200,
  231. height:200,
  232. marginTop:30
  233. }
  234. });
  235.  
  236. export default InputUsers;
  237.  
  238. <?php
  239. // Create database connection
  240. $db = mysqli_connect("localhost", "root", "", "image_upload");
  241.  
  242. // Initialize message variable
  243. $msg = "";
  244.  
  245.  
  246. // If upload button is clicked ...
  247. if (isset($_POST['upload'])) {
  248. // Get image name
  249. $image = $obj['image']['name'];
  250. // Get text
  251. $image_text = mysqli_real_escape_string($db, $_POST['image_text']);
  252.  
  253. // image file directory
  254. $target = "images/".basename($image);
  255.  
  256. $sql = "INSERT INTO images (image, image_text) VALUES ('$image', '$image_text')";
  257. // execute query
  258. mysqli_query($db, $sql);
  259.  
  260. if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
  261. $msg = "Image uploaded successfully";
  262. }else{
  263. $msg = "Failed to upload image";
  264. }
  265. }
  266. $result = mysqli_query($db, "SELECT * FROM images");
  267. ?>
Add Comment
Please, Sign In to add comment