I am trying to programmatically upload a file (and some fields) from the render process of an Electron application. I am using the form-data and axios packages for this purpose.
The code is TypeScript and, at its simplest, it looks like:
import axios from 'axios';
import fs from 'fs';
import FormData from 'form-data';
function upload(url: string, file: string, additionalFieldValue: string): void {
const f = new FormData();
f.append('additionalField', additionalFieldValue);
f.append(file, fs.createReadStream(file));
axios.post(url, f, {
headers: f.getHeaders(),
});
}
The problem I run into is that f
is always a native FormData
object instead of
FormData
object of the type declared in the form-data
package. As a consequence, it generates a TypeError: f.getHeaders is not a function
error.
The same thing happens if I change the import
statement to:
import SomeFormDataThatIsNotNative from 'form-data';
or if I try to use an alias:
import * as SomeFormDataThatIsNotNative from 'form-data';
and invoke the constructor like:
const f = new SomeFormDataThatIsNotNative();
I need help understanding why instantiating one type of object (FormData
declared in the form-data
package) results in a different type of object (native FormData
). What am I doing wrong? Do I have to move the code in the main process? What would work in the renderer process?