upd: announcement dan upload gambar
Deskripsi - fix html tag pada pengumuman - testing upload gambar = blm selesai No Issues
This commit is contained in:
@@ -14,7 +14,7 @@ import { useDispatch } from "react-redux";
|
||||
import * as FileSystem from 'expo-file-system';
|
||||
import axios from "axios";
|
||||
import ReactNativeBlobUtil from 'react-native-blob-util';
|
||||
import FormData from 'form-data';
|
||||
import { launchImageLibrary } from "react-native-image-picker";
|
||||
|
||||
const debug = true
|
||||
|
||||
@@ -151,52 +151,46 @@ const requestPermission = async () => {
|
||||
return true;
|
||||
};
|
||||
|
||||
const base64ToBlob = (base64: string, mimeType: string) => {
|
||||
const byteString = atob(base64);
|
||||
const arrayBuffer = new ArrayBuffer(byteString.length);
|
||||
const int8Array = new Uint8Array(arrayBuffer);
|
||||
|
||||
for (let i = 0; i < byteString.length; i++) {
|
||||
int8Array[i] = byteString.charCodeAt(i);
|
||||
}
|
||||
|
||||
return new Blob([int8Array], { type: mimeType });
|
||||
};
|
||||
|
||||
|
||||
async function kirim() {
|
||||
const hasPermission = await requestPermission();
|
||||
|
||||
if (!hasPermission) {
|
||||
return;
|
||||
}
|
||||
|
||||
let result = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||
aspect: [4, 3],
|
||||
quality: 0.8,
|
||||
console.log("clicked");
|
||||
const result = await launchImageLibrary({
|
||||
mediaType: "photo",
|
||||
quality: 1,
|
||||
includeBase64: true,
|
||||
});
|
||||
|
||||
if (!result.canceled) {
|
||||
const uri = result.assets[0].uri;
|
||||
console.log("Selected image URI:", uri);
|
||||
|
||||
try {
|
||||
|
||||
const base64Data = await ReactNativeBlobUtil.fs.readFile(uri, 'base64');
|
||||
|
||||
// Convert base64 to Blob
|
||||
const blob = new Blob([base64Data], { type: 'image/jpeg' });
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append('file', blob);
|
||||
formData.append('name', 'bg.png');
|
||||
|
||||
|
||||
console.log("Sending FormData with file URI...");
|
||||
|
||||
// Kirim request tanpa header Content-Type
|
||||
const response = await fetch("http://10.0.2.2:3000/api/v2/test", {
|
||||
method: 'POST',
|
||||
body: formData as any,
|
||||
});
|
||||
|
||||
// console.log("Response status:", response.status);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('Error uploading image:', error);
|
||||
return null;
|
||||
}
|
||||
if (!result.assets || result.assets.length === 0) {
|
||||
console.log("No image selected");
|
||||
return;
|
||||
}
|
||||
return null;
|
||||
|
||||
const base64 = result.assets[0].base64!;
|
||||
const mimeType = result.assets[0].type!; // e.g., 'image/jpeg'
|
||||
|
||||
const blob = base64ToBlob(base64, mimeType);
|
||||
|
||||
const formData = new FormData();
|
||||
formData.append("file", blob);
|
||||
formData.append("name", "bip.png");
|
||||
const res = await fetch("http://10.0.2.2:3000/api/v2/test", {
|
||||
method: "POST",
|
||||
body: formData,
|
||||
});
|
||||
console.log(await res.text());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user