I am trying to import data from excel file.
when i upload .csv file using livewire it convert to .txt file.
note: I am not sure if it is from Livewire or Laravel.
here is my modal code for importing file:
<x-jet-modal maxWidth="sm" wire:model="showImportDialog">
<form wire:submit.prevent="import" method="POST" enctype="multipart/form-data">
<div>
<div class="text-lg">Import Users</div>
</div>
<div>
<input type="file" wire:model="users" name="users" id="users" />
</div>
<x-jet-button type="submit">Import</x-jet-button>
</form>
</x-jet-modal>
and this is my livewire component:
...
use WithFileUploads;
public $users;
public function import()
{
dd($this->users); // originalName: "W4K99h75YVHSou815dV0fErWMLzZ75-metaUUNTX1VzZXJzXzIwMjEtMDktMTQgMTdfNDdfNTMuY3N2-.txt"
$validated = $this->validate([
'users.*' => 'required|file|mimes:xls,xlsx,csv',
]);
dd($validated); // it displays "[]"
}
...
when I dd() before validation shows this:
update 1:
when I did this:
dd($this->users->getClientOriginalName());
it shows:
Users_2021-09-14 17_47_53.csv
Update 2:
livewire config file:
Here is the explanation for this: How to store csv file laravel 5 php?
But basically, you have to specify the type of file using ->storeAs($path, $filename . '.csv').
Then laravel will save it like a CSV file; otherwise, it will turn the file into .txt.
Related
I am working on an Excel import module as part of a CRM for my company. I want to import an excel sheet. I use the Maatwebsite Excel package, version 3.1. I want to show the form and then upload a sheet. However I can't even get to that point. I have already determined the issue is within the form route, just not sure what it is exactly that I am missing.
Routes that I use to display the page (index works fine)
Form used to get the Excel sheet imported
Navigation bar link in the menu
DataController (from which I am trying to call the import method)
If you know what may be wrong please do tell, this is really frustrating!
Route code:
Route::get('importeren', 'Datacontroller#index');
Route::post('import', 'Datacontroller#import');
<div class="container-fluid">
<form action="import" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Imports\DataImport;
use Maatwebsite\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class DataController extends Controller
{
public function index(){
return view('importeren');
}
public function import(Request $request){
Excel::import(new DataImport(), $request->file('import_file'));
return redirect()->route('/home');
}
}
Don't use route() method because you're not defining any routes name, use something like:
form action="/import" method="POST" enctype="multipart/formdata">
you should use route() only with route name, use this instead :
return redirect('/home');
You can give a name to your route:
Route::post('import', 'Datacontroller#import')->name('import');
and leave the form action as it is with the route() helper:
<div class="container-fluid">
<form action="{{ route('import') }}" method="POST" enctype="multipart/form-data">
#csrf
<input type="file" name="import_file">
<br>
<input type="submit" value="Import">
</form>
</div>
You could also use the url() helper and leave the route with no name, but I highly recommend the option of giving your route a name.
<form action="{{ url('import') }}" method="POST" enctype="multipart/form-data">
Note that in the controller return you are also using a redirect to a named route, so I suggest that you give that route a name and use that name in the redirect. For example:
Route::get('home', 'SomeController#someMethod')->name('home');
and
return redirect()->route('home');
guys. I'm a newbie of Laravel.
I'm just wondering that if I need to pass some sensitive info, which is been wrote on a form, to controller, then to another view.
How could I pass the info to the view without using URL?
I have made some small test, but the result is all the same (url would include the data).
Here is the code.
Html(a):
<form action="/data" method="GET" class="form-horizontal">
<div class="form-group">
<input type="text" class="form-control" name="Test">
</div>
<div class="form-group">
<button type="submit" class="btn btn-primary">test</button>
</div>
web.php:
Route::get('/data', 'DataController#show');
DataController.php:
public function show(Request $request) {
return view('testShow');
}
Html(b):
<div class="ShowDataHere">
#if(!empty(request()->all()))
{{ request()->Test }}
#endif
Simply change your form to use POST instead of GET.
On the form...
<form action="/data" method="POST" class="form-horizontal">
In your route ...
Route::post('/data', 'DataController#show');
This will send all of the form fields to the server in a way that is less visable (not in the URL) but to make the form secure, you'll want to ensure it's served using HTTPS as well.
In Laravel when you use Route::post('/data', 'DataController#show') the Laravel expect a GET, because of the show function.
So you can use Route::post('/data', 'DataController#someFunction') and in your laravel controller get with:
public function someFunction(Request $request)
{
$array = $request->all(); //your fields
}
In this function you can get the attributes with $array = $request->all();
I am building a web app using angular2 for my frontend and lumen for my backend. I have a form where a user can upload a picture but I want to add validation so the user can only upload valid image formats (JPEG, PNG, GIF, etc) and the file cannot be larger than 32mb. I want to return a response to my frontend as an alert if any of these conditions are not met
The upload form (angular2)
<form ngNoForm action=""
target="_blank"
method="POST"
enctype="multipart/form-data">
<input type="file" name="image" id="image">
<input type="submit" value="Upload" name="submit">
</form>
How I'm saving it to the database
public function imageUpload(Request $request) {
// Check to make sure file is valid and doesnt exceed 32mb or display alert
$file = $request->file('image');
$imagedata = file_get_contents($file);
$base64 = base64_encode($imagedata);
if (DB::table('paint')->where('id', 1)
->update(['pic' => $base64]))
{
//Display alert on frontend after pic sucessfully uploaded
}
public function imageUpload(Request $request) {
$this->validate($request, [
'image' => 'required|image|max:32.896'
]);
//rest of your code
}
You can call the validate method at the top of your functions. If one condition will fail it will return an error you can display. its checking:
if the request contains the variable image
if it's an image type (jpeg, png, bmp, gif, or svg)
it's size is smaller or equal to 32.896 kb
for more rules you can check out: https://laravel.com/docs/5.4/validation
in your html file you can do something like this:
<div *ngFor="let elements of errors">
<div class="alert alert-warning" *ngFor="let element of elements">{{ element }}</div>
</div>
it depends how you want to display your messages.
I am trying to copy a file to my server Public Folder.
I am getting the following error :
BadMethodCallException in Macroable.php line 74: Method store does not exist.
This is the html to upload the file :
<form action="/leads/csvFiles" method="post" enctype="multipart/form-data">
{{csrf_field()}}
<input type="file" name="csvfile" />
<input type="submit"/>
</form>
And here is the Route:
Route::post('leads/csvFiles', function(){
request()->file('csvfile')->store('Public');
return back();
});
store() method has been implemented from Laravel 5.3, you need to use something like:
Route::post('leads/csvFiles', function(){
$request->file('csvfile')->move('Public');
return back();
});
Its advised to check first if file is valid:
if ($request->file('csvfile')->isValid()) {
//next code here
}
Then you you can actually save the file with whatever name you want.
$request->file('csvfile')->move('Public', 'myfilename.csv');
I am just using a form to upload a file and a simple route to move the file in my /public/images folder. But the moved file cant be found in the images directory. I am following Leanpub Laravel CodeBright book.
my blade template
<form action="{{ url('handle-form') }}"
method="POST"
enctype="multipart/form-data">
<input type="file" name="book" />
<input type="submit">
</form>
route.php
Route::get('/', function()
{
return View::make('form');
});
Route::post('handle-form', function()
{
Input::file('book')->move('/public/images');
return 'File was moved.';
});
After pressing submit button 'File was moved.' shown but there is no file in the desired directory.
I got it. I just remove leading '/' from the '/public/images' and its working fine.Now i am using 'public/images'.