Uploading file to local storage using Spring boot - spring-boot

I am trying to submit a form where i am trying to upload a file to a local storage . The below code works just fine and upload the file to the local storage .
<form method="POST" enctype="multipart/form-data" action="/uploadfile">
<table>
<tr><td>File to upload:</td><td><input type="file" name="file" /></td></tr>
<tr><td></td><td><input type="submit" value="Upload" /></td></tr>
</table>
</form>
But in the below code i am trying to use the form-group class and it is not even calling the controller to take the request and save the file to the local storage.
<form method="POST" enctype="multipart/form-data" action="/uploadfile">
<div class="custom-file form-group">
<input type="file" id="myfile" class="custom-file-input" multiple>
<label class="custom-file-label" for="myfile">Choose file</label>
</div>
<br/>
<div class="modal-footer">
<button class="btn btn-primary" data-dismiss="modal" type="submit">Submit</button>
</div>
</form>
What i should do to make the second part work.

Related

How to submit traditional blade form from livewire component?

I am trying to reuse laravel's default authentication components in livewire component. But when the login form is submitting I am getting a console error
wire-directives.js:86 Uncaught TypeError: Cannot read properties of
null (reading 'match')
The login form working perfectly fine when I am using the default login blade. But when I am reusing the login form in my livewire component, it is not working. Here is my code:
In my livewire component, I have this
<div>
<x-login-form/>
</div>
and the login form component is laravels default login form:
<form role="form" class="text-start" method="POST" action="{{ route('login') }}">
#csrf
<div class="input-group input-group-outline my-3">
<input type="email" id="email" class="form-control" name="email" :value="old('email')" required placeholder="email">
</div>
<div class="input-group input-group-outline mb-3">
<input type="password" class="form-control" id="password" type="password" name="password" required autocomplete="current-password" placeholder="password">
</div>
<div class="form-check form-switch d-flex align-items-center mb-3">
<input class="form-check-input" type="checkbox" id="remember_me" name="remember">
<label class="form-check-label mb-0 ms-2" for="rememberMe">Remember Me</label>
</div>
<div class="text-center">
<button type="submit" class="btn bg-gradient-primary w-100 my-4 mb-2">Sign In</button>
</div>
#if (Route::has('password.request'))
<p class="text-xs text-center">
Forgot your password?
</p>
#endif
</form>

LARAVEL_MethodNotAllowedHttpException

I read other answers about my question but they were useless for me.
I can't solve this error.
<form method="post" action="/cards/{{ $cards->id }}/notes">
<div class="form-group">
<textarea name="body" title="body" class="form-control" style="text-align: center"></textarea>
</div>
<div>
<button type="submit" class="btn btn-primary">Add Note</button>
</div>
</form>
and :
Route::get('cards', 'CardsController#index');
Route::get('cards/{cards}', 'CardsController#show');
Route::post('cards/{cards}/notes', 'NotesController#store');
thank for your help
You might need to add a CSRF token to your form. Try this:
<form method="post" action="/cards/{{ $cards->id }}/notes">
{{ csrf_field() }}
<div class="form-group">
<textarea name="body" title="body" class="form-control" style="text-align: center"></textarea>
</div>
<div>
<button type="submit" class="btn btn-primary">Add Note</button>
</div>
</form>

Uploading in laravel 5.6 not working

I've tried using this link https://www.5balloons.info/example-file-upload-validation-laravel-5-6/
And I manage to get it working by trying to return the FileName but the problem is the file I'm trying to upload is not showing in the folder "storage/app"
Here is my code
Controller
public function letterOfIntent(Request $request)
{
$fileName = "fileName".time().'.'.request()->fileToUpload->getClientOriginalExtension();
$request->fileToUpload->storeAs('logos',$fileName);
return 'success';
}
View
<form method="post" action="/supplier-information/company-officer/letter-of-intent" method="post" enctype="multipart/form-data" data-form-method="create">
{{ csrf_field() }}
<div class="row">
<div class="col-lg-offset-1 col-lg-4">
<label style="color:tomato;">Letter of Intent</label>
<input type="file" class="form-control-file" name="fileToUpload" id="exampleInputFile" aria-describedby="fileHelp">
<small id="fileHelp" class="form-text text-muted">Please upload a valid image file. Size of image should not be more than 2MB.</small>
</div>
<div class="col-lg-3">
<label>Date of Validity</label>
<input class="form-control" type="date" name="upload_file" id="upload_file" class="form-control">
</div>
<div class="col-lg-3">
<br>
<button type="submit" class="btn btn-primary btn-lg btn-flat text-contrail">Upload</button>
<button type="submit" class="btn btn-success btn-lg btn-flat text-contrail">View</button>
</div>
</div>
</form>

Input type file no file chosen after submit

I've just upload the file in the input type file, and when the value of the form are submitted, the input type file still empty
I emphasize that in my data base and my storage folder I have the registration line and the file.
<!-- my form-->
<form role="form" action="/infos/update" method="POST" enctype="multipart/form-data">
{{ csrf_field() }}
<div class="col-md-6">
<div class="form-group">
<label for="cin" class="control-label"> charger votre CNIE recto-verso en format pdf</label>
<!--input type file -->
<input name="pdfcine" type="file" accept="application/pdf" class="form-control underlined" required="required" value="{{$user->cineface1}}">
<div class="clearfix"></div></br>
<a class="btn btn-success" href="{{ url('/').'/storage/'. $user->cineface1 }}" target="_blank"><span class="glyphicon glyphicon-download-alt"></span>Download Cine</a>
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">Enregistrer</button>
</div>
</form>
You must include enctype="multipart/form-data" with form action.
You must be able to retrieve the file using either:
$request->file('pdfcine'); or
Input::get('pdfcine');

ajax file upload to a rest api

I have a form like below to upload a file.
<form id="formLicense" name="formLicense" enctype="multipart/form-data" class="form-horizontal" method="post" role="form">
<div class="modal-body">
<div id="tblUserData" >
<div class="space-4"></div>
<div class="form-group">
<div class="col-sm-9">
<input type="button" value="Select License" id="licenseFileButton" name="licenseFileButton" class="btn-minier btn btn-primary"><span id="selectedFile"></span>
<input style="display: none" type="file" name="licenseFile" id="licenseFile" size="50">
</div>
</div>
<div class="form-group">
<div class="col-sm-9">
<input type="checkbox" name="merge" value="true" class="ace "> <label class="lbl"> Merge with current license</label>
</div>
</div>
<div class="space-4"></div>
<div class="form-group">
<div class="col-sm-9">
<select name="destination">
<option value="1">path1</option>
<option value="2">path2</option>
<option value="3">path3</option>
</select>
</div>
</div>
<div class="space-4"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Cancel</button>
<button type="submit" class="btn btn-primary" id="licenseSave">Upload License</button>
</div>
</div>
</form>
I have a rest api to which i should pass this 3 form elements and it will do all file upload processing.
the api looks like below.
Path: POST /license/install
Request content type: multipart/form-data
Response content type: application/json
Parameters (HTML form):
licenseFile – file payload
merge – true if uploaded license should be merged with existing one, false for overwrite
destination:
1 - Path1
2 - path2
3 - path3
Now on save click I should via ajax I should be able to call this rest api and post all these 3 values to api and get response in success function and display it. I am not getting how I should do this. I saw many examples on net. but all are very complex. I dont need progress bar or anything. I want some thing simple which solve my this purpose. Your help will be much appreciated.

Resources