Uploading and storing file with Laravel - file

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);`

Related

Download zip file from different server location in react js

Folder structure on server:
-JAVA_PROJECT
---------DOWNLOAD_FOLDER
---------src
---------target
-REACT PROJECT
---------Public
---------src
---------node_modules
I have zip file in DOWNLOAD_FOLDER. Onclick of a button on react page i want to download the zip file.
My Code:
var urls = [
"/files/ProductList.zip",
];
var url = urls.pop();
var a = document.createElement("a");
a.setAttribute("href", url);
a.setAttribute("download", "");
a.setAttribute("target", "_blank");
a.click();
I am able to download only the files placed in PUBLIC folder. Is there any wway to traverse location to DOWNLOAD_Folder and download the zip file?
Or shall i fetch zip file content using axios and download file?
Please suggest.

File upload using ng-file-upload in spring mvc

I used ng-file-upload for uploading a file, but in my spring mvc controller with #RequestPart MultipartFile, the getOriginalFilename() method returns blob not the file name
#RequestPart(value = "imageFile", required = false) MultipartFile imageFile
The console log for my upload is:
{$ngfName: "UI-UX.jpeg", $ngfOrigSize: 133046, size: 6320, type: "image/jpeg"}
My file uploads correctly but I don't know why the file name is not correct.
Can you please help me?
Thanks.
Try with this
#RequestParam("image") MultipartFile image

Block direct downloads using Symfony

I want block the direct access to some of public files from my directory. For example, I have the file image.png and I have to fill a captcha to download that file, if I fail the captcha I don't want that the user could access the file. Is there any way to do that in Symfony?
First of all, create an .htaccess file inside the image directory to prevent the direct access of files inside the directory.
Deny from All
Now, give customised path, through controller to download the file, where you can easily integrate a captcha by using some bundle like Gregwar's CaptchaBundle.
On successful captcha validation, download the file through Response.
$filename = __DIR__ . "../path_to_file/image.png";
// Generate response
$response = new Response();
// Set headers
$response->headers->set('Cache-Control', 'private');
$response->headers->set('Content-type', mime_content_type($filename));
$response->headers->set('Content-Disposition', 'attachment; filename="' . basename($filename) . '";');
$response->headers->set('Content-length', filesize($filename));
// Send headers before outputting anything
$response->sendHeaders();
$response->setContent(file_get_contents($filename));
Note : Code not tested!
Hope this helps!

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

grails file download

I have sucessfully managed to make a file upload system which basically is copying files to a specific folder and save in the database its location. Now i need help with the download part. Imagine my file location is: Files/1306242602661_file1.exe, and in my view i have this:
<g:link controller="fileManager" action="downloadFile">
Download</g:link><br>
I need help with the downloadFile controller. Could you please give me a hint about how to do this, considering my filename is a string:
String fileName = "Files/1306242602661_file1.exe"
Within your controller create an download action with following content:
def file = new File("path/to/file")
if (file.exists()) {
response.setContentType("application/octet-stream")
response.setHeader("Content-disposition", "filename=${file.name}")
response.outputStream << file.bytes
return
}
// else for err message
You can render a file. see http://grails.org/doc/2.4.x/ref/Controllers/render.html
render file: new File ("path/to/file.pdf"), fileName: 'myPdfFile.pdf'

Resources