How to create Upload files in laravel 5.1? - database

I'm new to Laravel 5.1
Can you guys help me on how to upload files like docx, PDF or image to store it in the database using Laravel 5.1 ?
I browsed a lot of tutorials but not in Laravel 5.1 I'm trying it myself but it didn't work.
NotFoundHttpException in
C:\Loucelle\ _\
_\vendor\laravel\framework\src\Illuminate\Routing\RouteCollection.php line 161:
Can you help me by giving any sample codes?

Your Route:
Route::post('uploadFile', 'YourController#uploadFile');
Your HTML blade:
<form action="{{ url('uploadFile') }}" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit">
</form>
Your Controller:
public function uploadFile()
{
//get the file
$file = Input::file('file');
//create a file path
$path = 'uploads/';
//get the file name
$file_name = $file->getClientOriginalName();
//save the file to your path
$file->move($path , $file_name); //( the file path , Name of the file)
//save that to your database
$new_file = new Uploads(); //your database model
$new_file->file_path = $path . $file_name;
$new_file->save();
//return something (sorry, this is a habbit of mine)
return 'something';
}
Useful resources (these links may expire, so they are only here for reference):
Laravel Requests (like Inputs and the such)
File Upload Tutorial that helped me when I started

Related

ASP.Net Web Api & Angularjs: Multiple Files Upload [duplicate]

I need to upload image and video files to the server in an Angular application using Laravel 5.1 as the back end. All Ajax requests need to go to the Laravel controller first, and we have the code there for how the file gets handled when it gets there. We have previously done normal HTML forms to submit file uploads to the controller, but in this case we need to avoid the page refresh of a form, so I am attempting this in Ajax through Angular.
What information do I need to send to the Laravel controller with Ajax that was being sent to the controller via an HTML form previously?
This is the code in the Laravel controller that handled the file information once it got there. That's what I need to figure out how to send, so I can hopefully reuse this code:
$promotion = Promotion::find($id);
if (Input::hasFile('img_path')){
$path = public_path().'/images/promotion/'.$id.'/';
$file_path = $path.'promotion.png';
$delete = File::delete($file_path);
$file = Input::file('img_path');
$uploadSuccess = $file->move($path, 'promotion.png');
$promotion->img_path = '/images/promotion/'.$id.'/promotion.png';
}
if (Input::hasFile('video_path')){
$path = public_path().'/video/promotion/'.$id.'/';
$file_path = $path.'promotion.mp4';
$delete = File::delete($file_path);
$file = Input::file('video_path');
$uploadSuccess = $file->move($path, 'promotion.mp4');
$promotion->video_path = '/video/promotion/'.$id.'/promotion.mp4';
}
As you can see above, we are converting whatever file we get to a PNG with the file name promotion.png so it's easy to fetch, and we are only accepting .mp4 video format. Because of that, we don't need to worry about checking if the file exists and is it ok to overwrite it. That's why you can see in the code we delete any existing file of that name before saving.
The HTML was just an input with a type of "file:
<input type="file" id="img_path" name="img_path" class="promo-img-path" accept="image/*">
We are using Angular now so I can't just send the above through an HTML form anymore. That's what I need to figure out how to do.
We are two developers just doing our best, so I'm sure there is a better way of doing this. However before I refactor this whole thing, I'm hoping I can use Angular (or jQuery as a last resort) to just send the controller whatever file data Laravel needs in order to make the above code work. The answer may be as simple as "send a PUT to the method in that controller above, but instead of a normal JSON payload, use file info in this format and you can gather that info with..."
I would also appreciate any tips on better ways I can do this in the future.
How to POST FormData Using the $http Service
When using the FormData API to POST files and data, it is important to set the Content-Type header to undefined.
var fd = new FormData()
for (var i in $scope.files) {
fd.append("fileToUpload", $scope.files[i]);
}
var config = {headers: {'Content-Type': undefined}};
var httpPromise = $http.post(url, fd, config);
By default the AngularJS framework uses content type application/json. By setting Content-Type: undefined, the AngularJS framework omits the content type header allowing the XHR API to set the content type. When sending a FormData object, the XHR API sets the content type to multipart/form-data with the proper boundaries and base64 encoding.
For more information, see MDN Web API Reference - XHR Send method
How did you get the file information into $scope.files?
How to enable <input type="file"> to work with ng-model
This directive also enables <input type="file"> to automatically work with the ng-change and ng-form directives.
angular.module("app",[]);
angular.module("app").directive("selectFilesNg", function() {
return {
require: "ngModel",
link: function postLink(scope,elem,attrs,ngModel) {
elem.on("change", function(e) {
var files = elem[0].files;
ngModel.$setViewValue(files);
})
}
}
});
<script src="//unpkg.com/angular/angular.js"></script>
<body ng-app="app">
<h1>AngularJS Input `type=file` Demo</h1>
<input type="file" select-files-ng ng-model="fileArray" multiple>
<code><table ng-show="fileArray.length">
<tr><td>Name</td><td>Date</td><td>Size</td><td>Type</td><tr>
<tr ng-repeat="file in fileArray">
<td>{{file.name}}</td>
<td>{{file.lastModified | date : 'MMMdd,yyyy'}}</td>
<td>{{file.size}}</td>
<td>{{file.type}}</td>
</tr>
</table></code>
</body>
RECOMMENDED: POST Binary Files Directly
Posting binary files with multi-part/form-data is inefficient as the base64 encoding adds an extra 33% overhead. If the server API accepts POSTs with binary data, post the file directly.
See How to POST binary files with AngularJS (with DEMO)

Symfony 3 upload file - FileNotFoundException

Today i was found strange thing in file upload via symfony 3. I was trying upload file this way:
// get file
$uploadedFile = $request->files->get('uploadedFile', null);
// generate file name
$fileName = $this->getNewFileName();
// move file
$uploadedFile->move($path, $fileName.".".$uploadedFile->guessExtension());
But i get FileNotFoundException...
So i must do this: (Move method guessExtension out, before method move)
// get file
$uploadedFile = $request->files->get('uploadedFile', null);
// generate file name
$fileName = $this->getNewFileName().".".$uploadedFile->guessExtension();
// move file
$uploadedFile->move($path, $fileName);
Can anybody explain why?
EDIT:
File i has from classic <form> with enctype="multipart/form-data" with <input type="file">

get multiple files from request on Grails

Im using this but it saves only one file. I want to save multiple files.
Here is my code:
<input id="data" type="file" name="data" multiple="multiple"/>
and
def uploadSave() {
def document = request.getFile("data").each { file ->
log.debug(file.originalFilename)
}
What can I use to save all the files uploaded and print their original names? I tried to use MultipartFile but doesnt work. Help me, please.
MultipartFile data = request.getFile("data"){
println "File name: "+ ${data.orignalFileName}"
}
Have you tried using the uploadr plugin for grails?
https://grails.org/plugin/uploadr
no point re-inventing the wheel.
I would Like suggest you this jquery it one best file upload I ever come across as per as customization comes into picture they have solve all the problem ,you can upload the file download file and delete uploaded file
http://hayageek.com/docs/jquery-upload-file.php#doc
Hope this help you Thank's.
try this
def uploadSave() {
// notice "getFiles" instead of "getFile"
def document = request.getFiles("data")
document.each { file ->
println(file.getOriginalFilename()) // try this
//log.debug(file.originalFilename) // tthink this is causing the error
}
}

How to set upload URL and file upload path in ng-file-upload (Lightweight Angular directive to upload files)

I am trying to implement jsfiddle example http://jsfiddle.net/danialfarid/s8kc7wg0/112/
of Drag and drop file upload using angular js and
it is working fine until I am using there URL path " 'http://angular-file-upload-cors-srv.appspot.com/upload"
I have used exactly the same code they have provided in the jsfiddle , I have understand that it will upload file in upload folder so i have created a folder name "upload" with 777 permission.
I am facing only issue with Controller url path section so i have given that code below.
$scope.upload = function (files) {
if (files && files.length) {
for (var i = 0; i < files.length; i++) {
var file = files[i];
if (!file.$error) {
Upload.upload({
url: 'https://angular-file-upload-cors-srv.appspot.com/upload',
data: {
username: $scope.content,
file: file
}
If you run this URL directly on the browser it will show you empty json data "{"size":0}"
I have searched for it in many website but not found any explanation about it.
My Query :
what is the use of this URl
how i can create this in my node js application.
How i can set file upload path
I have also checked there GitHub for details but no help
https://github.com/danialfarid/ng-file-upload
Any help will be appreciated

Uploading and storing file with Laravel

I am using Laravel illuminate/html and I am trying to upload an image file and store it in /public in the laravel installation folder. I have got the image from the request:
$img = Request::file('img');
How can I store it in the public folder?
Thanks
You could do this in your controller:
Request::file('img')->move(base_path('public/uploads'));
Or if you wish to specify a generic filename or change filename
$newfilename = str_random(32) .time();
$ newfilename = $newfilename. ".". Request::file('img')->guessClientExtension();
Request::file('img')->move(base_path('public/uploads'), $newfilename);`

Resources