Introduction
In this tutorial, we will learn how to upload files, Images, or Videos using Asp.net Web API and ReactJS. ReactJS is an open-source JavaScript library used for creating user interfaces, particularly for SPA. It is also used for controlling the view layer for web and mobile applications.
Prerequisites
- A basic knowledge of ReactJS
- Visual Studio Code IDE should be installed on your system
- Visual Studio and SQL Server Management Studio
- Node and NPM installed
- Bootstrap
Create a ReactJS Project
Let’s create a React.js project using the following command.
npx create-react-app fileupload
Now open the newly created project in Visual Studio Code and install Bootstrap by using the following command.
npm install --save bootstrap
Now, open the index.js file and import Bootstrap.
import 'bootstrap/dist/css/bootstrap.min.css';
Now Install the Axios library by using the following command. Learn more about Axios
npm install --save axios
Now, go to the src folder and create a new component named 'fileupload.js', and add the following lines to this component.
import React from 'react';
import { post } from 'axios';
class Fileupload extends React.Component {
constructor(props) {
super(props);
this.state = {
file: '',
};
}
async submit(e) {
e.preventDefault();
const url = `http://localhost:61331/api/Uploadfiles/Uploadfile`;
const formData = new FormData();
formData.append('body', this.state.file);
const config = {
headers: {
'content-type': 'multipart/form-data',
},
};
return post(url, formData, config);
}
setFile(e) {
this.setState({ file: e.target.files[0] });
}
render() {
return (
<p className="container-fluid">
<form onSubmit={e => this.submit(e)}>
<p className="col-sm-12 btn btn-primary">
File Upload
</p>
<h1>File Upload</h1>
<input type="file" onChange={e => this.setFile(e)} />
<button className="btn btn-primary" type="submit">Upload</button>
</form>
</p>
)
}
}
export default Fileupload;
Now open the App.js file and add the following code.
import React from 'react';
import logo from './logo.svg';
import './App.css';
import Welcome from './Welcome';
import Fileupload from './upload';
function App() {
return (
<p className="App">
{/* <Welcome></Welcome> */}
<Fileupload></Fileupload>
</p>
);
}
export default App;
Create a Table in the Database
Open SQL Server Management Studio, create a database named "Employee," and in this database, create a table. Give that table a name like "Stotefiles".
CREATE TABLE [dbo].[Stotefiles](
[Id] [int] IDENTITY(1,1) NOT NULL,
[File] [nvarchar](max) NULL,
CONSTRAINT [PK_Files] PRIMARY KEY CLUSTERED
(
[Id] ASC
) WITH (
PAD_INDEX = OFF,
STATISTICS_NORECOMPUTE = OFF,
IGNORE_DUP_KEY = OFF,
ALLOW_ROW_LOCKS = ON,
ALLOW_PAGE_LOCKS = ON
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
Create a New Web API Project
Open Visual Studio and create a new project.









HIẾU LÊPosted Aug 20, 2021, 7:34 AM
Hi, thanks for post, how i get image from database to show in react
shiva kskPosted Dec 14, 2019, 4:54 AM
Stotefile sf = new Stotefile(); where to take this object any body know tell me
Sourav Kumar DasPosted Dec 4, 2019, 7:01 AM
Nice article.