Set Default Value for a Input File Form

How to set a value to a file input in HTML?

You cannot set it to a client side disk file system path, due to security reasons.

Imagine:

<form name="foo" method="post" enctype="multipart/form-data">
<input type="file" value="c:/passwords.txt">
</form>
<script>document.foo.submit();</script>

You don't want the websites you visit to be able to do this, do you? =)

You can only set it to a publicly accessible web resource as seen in this answer, but this is clearly not the same as a client side disk file system path and it's therefore useless in that context.

Set default value in input type file

Due to browser's security, You cannot just put default value to file input.

The better way is to check if the user selected a file before updating your records.

So on your update function:

public function update(){
// Make sure you didn't required the user to select file.
$attribute = [
'name' => $request->name,
'order' => $request->order
]
if($request->hasFile('pathheader')){
//If user select a file, upload the file
//Then you should update your record by
//adding fields to your update attribute.
$attribute['pathheader'] => $pathheader;

}
//otherwise, no changes will happen to your 'pathheader' column.
DB::table('yourtable')->where('id',$id)->update($attribute);
}

Putting default value in input type=file....

For security reasons, the value of a <input type=file> element can only be changed by a user. It's not possible to change the value through JavaScript or HTML.

Angular2 Reactive forms - Set default value for form input field (input type=file)?

Error says that it is not possible to set input programmatically for type='file'.

Only empty string is possible.

Many posts say that this is a security issue to change the input value for file type.

jQuery, Select Input FILE and also set it to another INPUT

default value for file input form when update image in laravel 5.8

You can't set default value for <input type="file"/> the best thing you can do is when you're updating books table check if request has image if it doesn't then you can skip updating image or reassign previous one

here is what it looks like

DB::table('books')->where('id_book',$request->id_book,$image_name)->update([
'title' => $request->title,
'image' => $request->hasFile('image') ? $image_name : $book->image,
'category' => $request->category,
'description' => $request->description
]);


Related Topics



Leave a reply



Submit