-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileUpload.tsx
189 lines (165 loc) · 4.98 KB
/
FileUpload.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import React, { useEffect, useState } from 'react';
import { StyleSheet, View, Text, Button, Image } from "react-native";
import { usePubNub } from 'pubnub-react';
import * as ImagePicker from 'expo-image-picker';
import Constants from 'expo-constants';
import * as Permissions from 'expo-permissions';
const channel = 'awesomeChannel';
export function FileUpload() {
const pubnub: any = usePubNub(); // pubnub as any to avoid missing type defs for file upload (they are coming soon)
const [ imageId, setImageId ] = useState('');
const [ imageUp, setImageUp ] = useState('');
const [ imageDown, setImageDown ] = useState('');
const [ uploading, setUploading ] = useState(false);
const [ downloading, setDownloading ] = useState(false);
const [ error, setError ] = useState('');
const [ fileUploadResult, setFileUploadResult ] = useState('');
let getPermissionAsync = async () => {
if (Constants.platform && Constants.platform.ios) {
const { status } = await Permissions.askAsync(Permissions.CAMERA_ROLL);
if (status !== 'granted') {
alert('Sorry, we need camera roll permissions to make this work!');
}
}
};
let pickImage = async () => {
try {
let result = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ImagePicker.MediaTypeOptions.All,
quality: .1,
});
console.log('pick image');
// console.log(JSON.stringify(result));
if (!result.cancelled) {
setImageUp(result.uri);
}
setError('none');
} catch (e) {
setError(e.message);
}
};
let uploadImage = async () => {
try {
setUploading(true);
const result = await pubnub.sendFile({
channel: channel,
message: { test: 'message', value: 42 },
file: {
uri: imageUp,
name: 'myFile2.jpg',
mimeType: 'image/jpeg',
},
});
setUploading(false);
setImageId(result.id);
setFileUploadResult(JSON.stringify(result));
} catch (e) {
let errorMessage = e + ' -- ' + JSON.stringify(e);
console.log(errorMessage);
setUploading(false);
setError(errorMessage);
setImageId('');
setFileUploadResult('');
}
};
let downloadImage = async () => {
try {
console.log('id', imageId);
setDownloading(true);
const result = await pubnub.downloadFile({
channel: 'awesomeChannel',
id: imageId,
name: 'myFile2.jpg'
});
let fileContent = await result.toBlob();
const reader = new FileReader();
reader.addEventListener("load", () => {
setImageDown('' + reader.result);
setDownloading(false);
}, false);
reader.addEventListener("error", (e) => {
console.log(e);
setDownloading(false);
}, false);
reader.readAsDataURL(fileContent);
} catch (e) {
console.log(e);
setDownloading(false);
setError(e.message);
}
};
useEffect(() => {
getPermissionAsync();
}, []);
return (
<View>
<Text style={styles.FileHeader}>File Upload</Text>
<View>
<Button title="Pick an image from camera roll" onPress={pickImage} >Pick Image</Button>
</View>
{ imageUp ?
<View>
<Text>Image To Upload</Text>
<Image style={styles.Image} source={{uri: imageUp}} />
<Text>{imageUp}</Text>
<Button title="Updload to PubNub" onPress={uploadImage} >Upload Image</Button>
<Text>Upload Result</Text>
{
uploading ?
<Text>Uploading ...</Text>
:
<Text>{ fileUploadResult }</Text>
}
{
fileUploadResult ?
<View>
<Button
title="Download File"
onPress={e => {
e.preventDefault();
downloadImage();
}}
>
</Button>
{
downloading ?
<Text>Downloading ...</Text>
:
undefined
}
{
imageDown ?
<View>
<Text>Image Downloaded</Text>
<Image style={styles.Image} source={{uri: imageDown}} />
<Text>DataURL</Text>
<Text>{imageDown.substr(0,100)} ... </Text>
</View>
:
undefined
}
</View>
:
undefined
}
</View>
:
undefined
}
<Text> </Text>
<Text>File Error:</Text>
<Text>{ error }</Text>
</View>
);
}
const styles = StyleSheet.create({
FileHeader: {
backgroundColor: 'blue',
color: 'white',
textAlign: 'left'
},
Image: {
width: 200,
height: 200
}
});