React-Native-Tips
React-Native-Tips copied to clipboard
Use same format in Typescript
Hi, I was wondering how to to the same thing in typescript. I get an error with this line:
data.append('photo', {
uri: photo.uri,
type: 'image/jpeg',
name: 'testPhotoName'
});
Because of a type mismatch:
Argument of type '{ uri: string; name: string; type: string; }' is not assignable to parameter of type 'string | Blob'.
Object literal may only specify known properties, and 'uri' does not exist in type 'string | Blob'.
The documentation of FormData.append
looks nothing like the code described above. Does anybody have an idea how to solve this?
I have the same issue with flowtype. It shows me three possible methods that do not fit my needs:
- append(name: string, value: string): void;
- append(name: string, value: Blob, filename?: string): void;
- append(name: string, value: File, filename?: string): void;
For the blob parameter use:
JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' });
JSON.stringify
didn't work for me. @josauder @challme28 Were you guys able to get this to work?
For anyone else that comes across this. @LorandP's answer didn't work, but it was close. I was getting JSON parse [EOF] errors. This is what I ended up with:
JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));
Probably not the best approach, but I spent far too long trying to find a better way.
Same issue, how to fix this?
@josauder did you solve the problem? How?
JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));
Isn't this basically just the same as type casting as any
?
JSON.parse(JSON.stringify({ uri: photo.uri, type: 'image/jpeg', name: 'testPhotoName' }));
Isn't this basically just the same as type casting as
any
?
exactly
It's better to cast to unknown and then to Blob.
{} as unknown as Blob
Same problem here... I got this error and I was unable to fix any other solution other than casting?
@witalobenicio same here,
const imgData = new FormData();
const imgData = new FormData();
imgData.append("files", {
uri: image,
name: "image.jpg",
type: "image/jpg",
} as unknown as Blob);
console.log("i am imgData", imgData);
the output is empty ( i am imgData FormData {}), the formData is empty