I am passing a File
object that comes from the browser (by dropping) to a function like this:
// .... logic here...
accept(file)
public static accept(file: File) {
console.log(file)
/* prints:
=> lastModified: 1555453309243
lastModifiedDate: Wed Apr 17 2019 01:21:49 GMT+0100 (GMT+01:00) {}
name: "test.txt"
path: "test.txt" --> as you can see there is a path variable in here.
size: 16
type: "text/plain"
webkitRelativePath: ""
__proto__: File
*/
console.log(file.name) // => prints 'test.txt'
console.log(file.size) // => prints '16'
console.log(file.path) // => !! error given. path does not exists in File object. Also IDE does not show this parameter in IDE autocomplete list.
}
Error given:
Property 'path' does not exist on type 'File'.ts(2339)
Why File
does not have path
parameter? How can I make sure path exists? Is there any other type for File
? This is a HTML5 drop.
If I do this:
public static accept(file: any) {
alert(file.path);
}
It shows the relative path:
For anyone else using react-dropzone
, it seems they're getting the typing for FileWithPath
from this library file-selector
See line 3 here: https://github.com/react-dropzone/react-dropzone/blob/master/typings/react-dropzone.d.ts
You can simply import this FileWithPath
from react-dropzone
wherever you need it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With