Upon trying to upload a file using the formData.append(key, value);
, an error message is displayed in the value
section:
The argument of type 'unknown' cannot be assigned to a parameter of type 'string | Blob'. Type '{}' is missing essential properties such as size, type, arrayBuffer, slice, and more.
Code snippet
const uploadSubtitle = async e => {
e.preventDefault();
const file = fileInput.current.files[0];
const res = await Axios.get(`/api/movies/${currentMovie.movieId}/subtitles?user=${user.id}`);
const { url, fields } = res.data;
const newUrl = `https://${url.split('/')[3]}.s3.amazonaws.com`;
const formData = new FormData();
const formArray = Object.entries({ ...fields, file });
formArray.forEach(([key, value]) => {
formData.append(key, value);
});
//... further code
};
<form onSubmit={uploadSubtitle}>
<input type='file' name='subtitle' ref={fileInput} accept='.srt' />
<button onClick={uploadSubtitle}>Upload</button>
</form>
Extra information
console.log(file)
output displays
File
{name: "Trainspotting-English.srt", lastModified: 1587840529000,
lastModifiedDate: Sun Apr 26 2020 00:18:49 GMT+0530 (India Standard Time),
webkitRelativePath: "", size: 103040, …}
lastModified: 1587840529000 lastModifiedDate: Sun Apr 26 2020 00:18:49 GMT+0530 (India Standard Time)
{} name: "Trainspotting-English.srt" size: 103040
type: "application/x-subrip" webkitRelativePath: "" __proto__: File
console.log(typeof file)
reveals that it is
object