This is the image I need as a output can anybody let me know any solution to this. Here I want the file path to display in place of "set output path" I have already given the css to the button and its uploading the file but I need the path or file name at the place of "set output path"
Function DataExtractor(){
const uploadFiles=()=>{
document.getElementById('selectFile').click()
}
return(
<div>
<label>Set Output Path:</label>
<input type="text" placeholder="Set output path">
</div>
<div>
<button className="btn-1" onClick={uploadFiles}>Browse path</button>
<input type="file" id="selectFile" style={{display:"none"}}/>
</div>)}
)}
export default DataExtractor
You can access the file name as e.target.files[0] in onChange function. Something like this:
function DataExtractor() {
const [selectedFile, setSelectedFile] = useState('')
const uploadFiles = () => {
document.getElementById('selectFile').click()
}
const handleFileChange = (e) => {
setSelectedFile(e.target.files[0])
}
return (
<>
<div>
<label>Set Output Path:</label>
<input value={selectedFile} type="text" placeholder="Set output path">
</div>
<div>
<button className="btn-1" onClick={uploadFiles}>Browse path</button>
<input type="file" id="selectFile" style={{display:"none"}} onChange={handleFileChange} />
</div>
</>
)
}
Related
My page has many js-generated form elements which can exist simultaneously but need to be created from the same block of code (jsx, react). My problem is that I need to give labels to all these form elements for semantics and seo, but I can't give them all the same id (becuase html ids are unique). Is the only option to dynamically generate unique html ids? Is there any way to do this with html classes?
My code would look something like this:
function cardEditor() {
return (
<form>
<label for="card-title">Title: </label>
<input type="text" id="card-title">
<button type="submit">Save</button>
</form>
);
}
You can pass id as props to your component
const Input = ({ id }) => {
return <>
<label for={id}>Title: </label>
<input type="text" id={id}>
</>
}
function cardEditor() {
return (
<form>
<Input id="card-title-1" />
<Input id="card-title-2" />
<Input id="card-title-3" />
<button type="submit">Save</button>
</form>
);
}
or generate unique ids for your form elements
const uuid = () => Math.random().toString(36).slice(-6);
const Input = () => {
const id = uuid();
return <>
<label for={id}>Title: </label>
<input type="text" id={id}>
</>
}
function cardEditor() {
return (
<form>
<Input/>
<Input/>
<Input/>
<button type="submit">Save</button>
</form>
);
}
I need to create a newline when enter is hit and submit the form only when clicked on button.
<form className="message_form" onSubmit={handleSubmit} >
<div>
<input
type="text"
placeholder="Enter message"
value={text}
onChange={(e) => setText(e.target.value)}
/>
</div>
<div>
<button className="btn">Send</button>
</div>
</form>
You can use a textarea instead of an input box and add the click event on the Send button. It'll look something like this:
import React, { useState } from "react";
import './App.css'
const App = () => {
const [text, setText] = useState("");
const handleSubmit = (e) => {
e.preventDefault();
console.log("text", text);
};
return (
<form className="message_form">
<div>
<textarea
type="text"
placeholder="Enter message"
rows={4}
value={text}
onChange={(e) => {
setText(e.target.value);
}}
>
{text}
</textarea>
</div>
<div>
<button className="btn" onClick={handleSubmit}>
Send
</button>
</div>
</form>
);
};
export default App;
You can check here for more info, it's a similar kind of question. Might help you further - Similar question on SO
To make the foem submit on a button click, you need to remove the onSubmit from tag and then make your button type="submit" . this will submit the form iff button is clicked. And for enter you need to capture the key press event ( assuming you are talking about enter while typing in the input field ) as ( this code will go in your onChangeHandler for input field:
if (e.key === 'Enter') {
setMakeTheLineVisible(true)
}
After getting the enter key you can toggle the state which will make the line you want visible on the page.
I have been trying to upload a simple image using adonisjs and the request.file() keeps on returning null.
I am pretty new to adonisjs and the documentation is not clear on what to do.
I am using a SQLite database.
This is my controller.
public async update({response, request}) {
let user = request.only(["userId","fullname","avatar"]);
const coverImage = request.file('avatar')
console.log(coverImage)
console.log(user)
if (!coverImage) {
return "Please upload File"
}
const imageName = new Date().getTime().toString()+ '.' + coverImage.subtype
await coverImage.moveAll(Application.publicPath('images'),{
name: imageName
})
user.avatar = `images/${imageName}`
await user.save()
return response.redirect(`/users/${user.userId}`)
}
This is my form that I am submitting the image with.
<form class="uk-grid-small" uk-grid method="post" action="{{ route('profiles.update', {id: user.id}) }}?_method=PUT">
<div class="uk-width-1-2#s">
<input class="uk-input" type="text" placeholder="Fullname" name="fullname">
</div>
<div class="uk-width-3-4#s">
<label>Upload user avatar</label>
<input type="file" multiple name="avatar" class="form-control">
</div>
<div class="uk-width-1-2#s">
<button class="uk-button uk-button-primary">Submit</button>
</div>
</form>
This is the route I am using
Route.resource('profiles', 'ProfilesController')
AdonisJS provides you a robust and performant API for dealing with file uploads. Not only can you process and store uploaded files locally, but you can also stream them directly to the cloud services like S3, Cloudinary, or Google cloud storage.
Accessing uploaded files
The bodyparser middleware registered inside the start/kernel.ts file automatically processes all the files for multipart/form-data requests.
You can access the files using the request.file method. The method accepts the field name and returns an instance of the File class, or null if no file was uploaded.
try this code
<form action="{{ route('posts.store') }}" method="POST" enctype="multipart/form-data">
<div class="row">
<div class="col">
<div class="custom-file">
<input type="file" class="custom-file-input" name="image_url" id="validatedCustomFile" required>
<label class="custom-file-label" for="validatedCustomFile">Choose file...</label>
<div class="invalid-feedback">Example invalid custom file feedback</div>
</div>
</div>
</div>
<button type="submit" class="btn btn-success mt-2">Submit</button>
</form>
controller
const postImage = request.file('image_url', {
size: '2mb',
extnames: ['jpg', 'png'],
})
let date_ob = new Date();
if (!postImage) {
return
}
if (!postImage.isValid) {
return postImage.errors
}
if (postImage) {
await postImage.move(Application.publicPath('postImage'), {
name: ("0" + date_ob.getDate()).slice(-2)+("0" + (date_ob.getMonth() + 1)).slice(-2)+date_ob.getFullYear()+date_ob.getHours()+date_ob.getMinutes()+date_ob.getSeconds()+date_ob.getMilliseconds()+'.'+postImage.extname,
overwrite: true, // overwrite in case of conflict
})
}
I am using reactjs and in my render I have this
<div className="file-field input-field col s12">
<div className="btn">
<span>File</span>
<input type="file" accept=".csv" onChange={ (event) => this.fileUploaded(event) }/>
</div>
<div className="file-path-wrapper">
<input className="file-path validate" type="text" placeholder="Upload A Signle csv file" />
</div>
I then have
fileUploaded(event) {
var reader = new FileReader();
}
but I don't see anything about files in event. Am I passing in the wrong thing?
Well you need to reference the element which has the file list.
console.log(event.target.files)
I use HTMLServices with google.script.run calls with form in args and i try to get the image from my form input file field and store it in my drive.
I get the value of all the fields correctly except the photodata field.
photodata contain the string "FileUpload" instead of a blob.
I have tested with .getBlob for the same result.
Server Side :
function setUsager(form) {
var blob = form.photodata;
Logger.log(Utilities.jsonStingify(blob);
DriveApp.getFolderById('0B2DdOMvW2Q84N0s4a21LM05wbms').createFile(form.usagerId,blob);
obj = new {};
obj.photo = file.getUrl();
return obj;
}
on click Handler
function onUsagerEditFormSubmit() {
loading(true);
var form = this.parentNode;
google.script.run.withSuccessHandler(showUsager)
.withFailureHandler(showError)
.setUsager(form);
}
And the form :
<form name="usager-edit" id="usager-edit" method='post' enctype="multipart/form-data">
<div class="hidden">
<input type="text" name="type" id="type" value="USAGER" readonly="readonly" />
<input type="text" name="usagerId" id="usagerId" readonly="readonly" />
</div>
<div class="field-container">
<label for="nom">Nom</label>
<input type="text" name="nom" id="nom">
</div>
<div class="field-container">
<label for="prenom">Prenom</label>
<input type="text" name="prenom" id="prenom">
</div>
<div class="field-container">
<label for="photo">Photo</label>
<input class="file" type="file" name="photodata" id="photodata" accept='image/*'>
<div id="prev-photo" name="prev-photo"></div>
<div class="clear"></div>
</div>
<input class="myButton" onClick="onUsagerEditFormSubmit" type="button" name="save" id="save-button" value="Sauvegarder" />
</form>
You are using the wrong createFile method overload.
You are using createFile(name, content), when you should use createFile(blob).
...
var file = DriveApp.getFolderById('****************************').createFile(blob);
...
You must define file in order to make: obj.photo = file.getUrl();.
Also you can change the following:
...
<input class="myButton" onClick="onUsagerEditFormSubmit" type="button" name="save" id="save-button" value="Sauvegarder" />
...
by:
...
<input class="myButton" onClick="onUsagerEditFormSubmit(this.parentNode)" type="button" name="save" id="save-button" value="Sauvegarder" />
...
and
function onUsagerEditFormSubmit() {
loading(true);
var form = this.parentNode;
google.script.run.withSuccessHandler(showUsager)
.withFailureHandler(showError)
.setUsager(form);
}
by:
function onUsagerEditFormSubmit(form) {
loading(true);
google.script.run.withSuccessHandler(showUsager)
.withFailureHandler(showError)
.setUsager(form);
}
Slight changes:
...
Logger.log(Utilities.jsonStingify(blob);
...
obj = new {};
...
by:
...
Logger.log(Utilities.jsonStringify(blob));
...
var obj = {};
...