Hi I am very new to React.js and typescript.I am trying to upload a simple file along with simple inputs.
I examined some examples(ex1,ex2,ex3) related with file uploading or input element events on the web but I couldn't make any of them to work.
Here is my tsx file.
import * as React from 'react';
//import * as $ from "jquery";
import { RouteComponentProps } from 'react-router';
import { Button, Form, FormGroup, Label, Input, FormText } from 'reactstrap';
interface UploadContainerState {
tcknVkn: number;
proclamationYear: number;
file: FileList|null;
}
export default class UploadContainer extends React.Component<{}, UploadContainerState> {
constructor(props: any) {
super(props);
this.state = {
proclamationYear: 0,
tcknVkn: 0,
file: null
}
this.proclamationYearChange = this.proclamationYearChange.bind(this);
this.tcknVknChange = this.tcknVknChange.bind(this);
this.uploadFile = this.uploadFile.bind(this);
this.fileChange = this.fileChange.bind(this);
}
proclamationYearChange(event: any) {
this.setState({ proclamationYear: event.target.value });
}
tcknVknChange(event: any) {
this.setState({ tcknVkn: event.target.value });
}
fileChange(event: any) {
this.setState({ file: event.target.files[0] });
}
render() {
return (
<div>
<form className="uploader" encType="multipart/form-data" >
<br />
<FormGroup>
<Label for="proclamationYear">Beyanname Yılı</Label>
<Input type="text" name="proclamationYear" id="proclamationYear" placeholder="Beyanname Yılını Giriniz." onChange={this.proclamationYearChange} value={this.state.proclamationYear} />
</FormGroup>
<FormGroup>
<Label for="tcknVkn">Tckn/Vkn</Label>
<Input type="text" name="tcknVkn" id="tcknVkn" placeholder="Tckn/Vkn Giriniz." onChange={this.tcknVknChange} value={this.state.tcknVkn} />
</FormGroup>
<input type="file" name="file" className="upload-file" onChange={this.fileChange} value={this.state.file} />
<input type="button" value="Beyanname Yükle" onClick={this.uploadFile} />
</form>
</div>
);
}
uploadFile(event: any) {
event.preventDefault();
let formData = new FormData();
formData.append('file', event.target.myimage.files[0]);
formData.append('tcknVkn', event.target.tcknVkn.value);
formData.append('proclamationYear', event.target.proclamationYear.value);
fetch('ProposalData/UploadFile', {
method: 'POST',
credentials: 'include',
headers: {
'Content-Type': 'multipart/form-data',
'Accept': 'application/json'
},
body: formData
})
}
};
I am receiving following error.
ERROR in [at-loader] ./ClientApp/components/UploadContainer.tsx:50:29
TS2322: Type '{ type: "file"; name: "file"; className: "upload-file"; onChange: (event: any) => void; value: Fi...' is not assignable to type 'DetailedHTMLProps<InputHTMLAttributes<HTMLInputElement>, HTMLInputElement>'.
Type '{ type: "file"; name: "file"; className: "upload-file"; onChange: (event: any) => void; value: Fi...' is not assignable to type 'InputHTMLAttributes<HTMLInputElement>'.
Types of property 'value' are incompatible.
Type 'FileList | null' is not assignable to type 'string | number | string[] | undefined'.
Type 'null' is not assignable to type 'string | number | string[] | undefined'.
Here is my typescript version 5.6.0
I can't understand how can such easy task becomes a nightmare.