Skip to content

Commit 4eda1c4

Browse files
committed
fix(mobile/uploads): read picked image as bytes
RN's fetch(file://).blob() can come back as size 0 on Android, making the PUT succeed but storing an empty object — which looks to the user like "upload worked but the photo won't load." Switch to arrayBuffer + Uint8Array for a reliable body. Keep a temporary size / status log to confirm in Metro.
1 parent acc69c5 commit 4eda1c4

1 file changed

Lines changed: 21 additions & 3 deletions

File tree

apps/mobile/features/uploads/hooks/use-photo-upload.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,16 +50,34 @@ async function uploadAsset(
5050
contentType,
5151
});
5252

53-
// Convert the local file URI into a Blob the upload expects.
53+
// Read the picked file's raw bytes. Using arrayBuffer() instead of blob()
54+
// avoids a React Native quirk where blob() can come back with size 0 on
55+
// Android, which makes the PUT succeed but store an empty object.
5456
const fileResponse = await fetch(asset.uri);
55-
const blob = await fileResponse.blob();
57+
const arrayBuffer = await fileResponse.arrayBuffer();
58+
const bytes = new Uint8Array(arrayBuffer);
59+
60+
console.log(
61+
"[upload] asset.uri=",
62+
asset.uri,
63+
"size=",
64+
bytes.byteLength,
65+
"type=",
66+
contentType,
67+
);
68+
69+
if (bytes.byteLength === 0) {
70+
throw new Error("Picked image came back empty; pick it again.");
71+
}
5672

5773
const putResponse = await fetch(uploadUrl, {
5874
method: "PUT",
5975
headers: { "Content-Type": contentType },
60-
body: blob,
76+
body: bytes,
6177
});
6278

79+
console.log("[upload] PUT status=", putResponse.status, "public=", publicUrl);
80+
6381
if (!putResponse.ok) {
6482
throw new Error(
6583
`Upload failed (${putResponse.status}): ${await putResponse.text()}`,

0 commit comments

Comments
 (0)