return file response in laravel not working - laravel-5

I'm using laravel 5.2 , I've used response()->file() function to return file. On localhost it is working as expected but on live server file is being downloaded automatically (with no extension). But i wish to open it instead of downlod. Anyone can help?
Here is my code:
public function returnFile($slug)
{$file = Mixes::where('id_name,'=',$slug)->get()->first();
return response()->file('./path/to/file/'.$file->name);}
Thanks.

You'll need to add header to your response.
Response with header example:
$response->header('Content-Type', 'application/pdf');
Simple example:
$file = File::get($file);
$response = Response::make($file, 200);
$response->header('Content-Type', 'application/pdf');
return $response;
Then the file will display in your browser window.
This solution will work with files pdf,docx,doc,xls.
Hope it will help!

Related

Laravel Api Postman Upload Image Return Null

$files = $request->file('images'); // return {}
$_FILES['images']; // return {"name":"sample-passport.jpg","type":"image\/jpeg","tmp_name":"D:\\xampp\\tmp\\php4AD9.tmp","error":0,"size":264295}
Have you tried to remove the Content-Type header? According to this Github issue, it seems to be a problem.
So, I set up a new Laravel installation to test this out and it's working fine on my side. Of course, there's no authorisation whatsoever but this shouldn't impact the result too much.
routes/api.php
Route::post('/profile/upload_image', function (Request $request) {
dd($request->file('image'));
});
Postman configs
Your post input attribute type change file after upload you will get a response.enter image description here

getting error while uploading file using postman

I'm trying to upload an image file . When i am calling my API to upload file using postman , getting error like Fatal error: Call to a member function file() on array.
I could not post this file to my Laravel controller .How to post files in Laravel. Anyone could help me to solve this issue?
Here is my controller function,
public function edit(Request $request){
$request = $request->input();
if(empty($request)) {
$request = json_decode(file_get_contents('php://input'),true);
}
$to_return = array();
$file = $request->file('files');
}
at this line $file = $request->file('files'); getting fatal error.
set method as post .. and at body ... change to option from form data to w-xxx-formurlencod ... and try ..
If you want to file uploads with Postman and Laravel, simply remove the Content-Type header setting in Postman.
set method as post
and at body ... select the radio that shows url encoded
see an image ... click on text set it as flie

shows me blank page when upload a .docx document on a website laravel

Hello I am new to laravel and still learning it and I come across different problems and issues and I cant find a way to solve it. I have been stuck to this since very long. Any help would be much appreciated.
The Problem
Whenever I upload a .docx file to my website i get a blank page without any error or without the content of that docx file. I dont know what the problem is. Please help me out with this.
Code
public function getUploadedFile() {
// $destinationPath = 'uploads';
// $path= public_path()."/". $destinationPath;
// $content = utf8_encode(File::get('/var/www/html/asad/File-Uploader/public/uploads/detailed_period_report_template.xls'));
// return view('/files', compact('path'));
$file = $this->request->file('file_name');
$file_name = $this->request->file_name;
if (File::isFile($file_name))
{
$file_name = File::get($file_name);
$response = Response::make($file_name, 200);
$content_types = [
'application/octet-stream', // txt etc
'application/msword', // doc
'application/vnd.openxmlformats-officedocument.wordprocessingml.document', //docx
'application/vnd.ms-excel', // xls
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', // xlsx
'application/pdf', // pdf
];
// using this will allow you to do some checks on it (if pdf/docx/doc/xls/xlsx)
$response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
return $response;
}
I dont have any view for this. Do I need one for this ? If yes what would be it like because every docx file has it's set of properties like font, size, etc unlike excel sheets where we just defines columns. Please help me out. Thank you in advance
I have figured it out after so long finally.
It was sending "null" as a file name so what I did now is got the file name from the database instead of storage and then compared it that if that file is in storage and database then open it. Below is the code running for me now.
Thank you #MATEY for all your help though
$upload = new Upload;
$file= public_path(). "/uploads/" . $upload->file_name = $this->request->file_name;
//dd($file);
$files = File::get($file);
$response = Response::make($files, 200);
and changed the content type to $response->header('Content-Type', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document');
Thank you for the all help #MATEY. You gave me the motivation to fix it plus your else{...} part missing in if condition actually helped me alot finding out the error.

return dynamic image zf2

I use zend framework 2 and try to return an created with gd2 library jpeg image . but it doesn't work. could you look my code what's the problem? My code is run with plain php in normally but in zf2 problem?
class PictureController extends AbstractActionController
{
public function colorPaletteAction(){
....
....
//canvas created at above.
imagejpeg($canvas);
imagedestroy($canvas);
$response = $this->getResponse();
return $response->getHeaders()->addHeaderLine('Content-Type', 'image/jpeg');
}
}
imagejpeg outputs the data immediately which you don't want to do. You can either use the output buffer to capture this data or write it to a file first. The output buffer is probably easiest:
public function colorPaletteAction()
{
// [create $canvas]
ob_start();
imagejpeg($canvas);
$imageData = ob_get_contents();
ob_end_clean();
imagedestroy($canvas);
$response = $this->getResponse();
$response->getHeaders()->addHeaderLine('Content-Type', 'image/jpeg');
$response->setContent($imageData);
return $response;
}
If this doesn't work, temporarily comment out the Content-Type header line to see what output you're getting. Make sure there aren't any errors or HTML in the output.
You set the Content-Type header to 'image/png' instead of 'image/jpeg'.
Also try adding the content-transfer-encoding and content-length headers:
$response->getHeaders()->addHeaderLine('Content-Transfer-Encoding', 'binary')
->addHeaderLine('Content-Length', mb_strlen($yourJpegContent));
I also don't see you adding the actual content to the response:
$response->setContent($yourJpegContent);
where $yourJpegContent contains the binary image data.

response::download is doing nothing

I am having problems getting files to download outside of the public folder. Here is the Scenario:
These files cannot be accessed from anywhere but through this application and these downloads must go through access control. So a user can't download the file if they are not logged in and have permission to.
I have a route defined with a get variable. This ID goes into the controller and the controller calls the method below:
public static function downloadFile($id){
$file = FileManager::find($id);
//PDF file is stored under app/storage/files/
$download= app_path().substr($file->location,6);///home/coursesupport/public_html/app/storage/files/fom01/7-2/activities-and-demos.pdf
$fileName = substr($download,strrpos($download,'/')+1);//activities-and-demos.pdf
$mime = mime_content_type($download);//application/pdf
$headers = array(
'Content-Type: '.$mime,
"Content-Description" => "File Transfer",
"Content-Disposition" => "attachment; filename=" . $fileName
);
return Response::download($download, $fileName, $headers);
}
the problem is it does nothing, just opens a blank page. Am I missing something? oh yeah, and the link to the route mentioned above opens a blank tab.
I appreciate any help. Thanks!
It should work by doing only this:
public static function downloadFile($id)
{
$file = FileManager::find($id);
$download= app_path().substr($file->location,6);
$fileName = substr($download,strrpos($download,'/')+1);
return Response::download($download, $fileName);
}
Make sure your route is actually returning the returned response. Eg:
Route::get('download', function()
{
return DownloadHandler::downloadFile(1);
});
Without the return in the route the IlluminateResponse will do nothing.

Resources