Commit 2b292b16 by Vijay Gawariya

create upload file api

parent 3c26d308
...@@ -26,6 +26,12 @@ class Api::V1::EmployeesController < ApplicationController ...@@ -26,6 +26,12 @@ class Api::V1::EmployeesController < ApplicationController
render json: { message: 'Employee deleted!' } render json: { message: 'Employee deleted!' }
end end
def uploadFile
# print{'aaaaaaaaaaaaaaaa'}
print(params.permit(:file),'aaaaaaaaaaaaaaaaa')
render json: {name: 'vijay'}
end
private private
def employee_params def employee_params
......
...@@ -10,11 +10,14 @@ class NewEmployee extends React.Component { ...@@ -10,11 +10,14 @@ class NewEmployee extends React.Component {
email: "", email: "",
phone: "", phone: "",
dob: "", dob: "",
address: "" address: "",
file: {}
}; };
this.onChange = this.onChange.bind(this); this.onChange = this.onChange.bind(this);
this.uploadFile = this.uploadFile.bind(this);
this.onSubmit = this.onSubmit.bind(this); this.onSubmit = this.onSubmit.bind(this);
this.submitFile = this.submitFile.bind(this)
this.stripHtmlEntities = this.stripHtmlEntities.bind(this); this.stripHtmlEntities = this.stripHtmlEntities.bind(this);
} }
...@@ -29,6 +32,43 @@ class NewEmployee extends React.Component { ...@@ -29,6 +32,43 @@ class NewEmployee extends React.Component {
this.setState({ [event.target.name]: event.target.value }); this.setState({ [event.target.name]: event.target.value });
} }
uploadFile(event) {
this.setState({ [event.target.name]: event.target.files[0] });
}
submitFile(event) {
event.preventDefault();
const url = "/api/v1/employees/uploadFile";
const { file } = this.state;
if (file.length == 0)
return;
const body = {
file
};
console.log(body);
const token = document.querySelector('meta[name="csrf-token"]').content;
const formData = new FormData()
formData.append("file", file)
fetch(url, {
method: "POST",
headers: {
"X-CSRF-Token": token,
// "Content-Type": "application/json"
},
body: formData
})
.then(response => {
if (response.ok) {
alert("AAAAAAAAAAA")
return response.json();
}
throw new Error("Network response was not ok.");
})
.catch(error => console.log(error.message));
}
onSubmit(event) { onSubmit(event) {
event.preventDefault(); event.preventDefault();
const url = "/api/v1/employees/create"; const url = "/api/v1/employees/create";
...@@ -72,6 +112,21 @@ class NewEmployee extends React.Component { ...@@ -72,6 +112,21 @@ class NewEmployee extends React.Component {
<h1 className="font-weight-normal mb-5"> <h1 className="font-weight-normal mb-5">
Add New Employee Details Add New Employee Details
</h1> </h1>
<form onSubmit={this.submitFile}>
<div className="form-group">
<label htmlFor="file">Upload File</label>
<input
type="file"
name="file"
className="form-control"
required
onChange={this.uploadFile}
/>
</div>
<button type="submit" className="btn custom-button mt-3">
Submit File
</button>
</form>
<form onSubmit={this.onSubmit}> <form onSubmit={this.onSubmit}>
<div className="form-group"> <div className="form-group">
<label htmlFor="employeeFirstName">First Name</label> <label htmlFor="employeeFirstName">First Name</label>
......
...@@ -5,6 +5,7 @@ Rails.application.routes.draw do ...@@ -5,6 +5,7 @@ Rails.application.routes.draw do
post 'employees/create' post 'employees/create'
get '/show/:id', to: 'employees#show' get '/show/:id', to: 'employees#show'
delete '/destroy/:id', to: 'employees#destroy' delete '/destroy/:id', to: 'employees#destroy'
post 'employees/uploadFile'
end end
end end
namespace :api do namespace :api do
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment