I am using the following code do download a zip file. I am sure the file is existing and working on the server. The result is .cpgz file after extraction.
return response()->download('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip');
The code was working and without any change, it stopped.
I also tried adding headers:
$headers = array(
'Content-Type' => 'application/zip',
);
return response()->download('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip', basename('/Applications/XAMPP/xamppfiles/htdocs/stoproject/source/storage/app/Copy of StoTherm Classic®.zip'), $headers);
Also tried with:
'Content-Type' => 'application/octet-stream'
Calling ob_end_clean() fixed the issue
$response = response()->download($pathToFile)->deleteFileAfterSend(true);
ob_end_clean();
return $response;
Laravel 4 Response download issue
Related
I have switched my Laravel 5.1 to HTTPS and everything seems fine, except the file download part.
The problem is response()->download() returns a HTTP link instead of HTTPS and I get a mixed content in Chrome, so the link is blocked.
And some code:
$headers = array
(
'Content-Type' => 'application/vnd.android.package-archive'
);
return response()->download(config('custom.storage') . $apk->generated_filename, $apk->filename, $headers);
I am getting a response from an external api which contains an array of bytes I'd like to convert to pdf.
Sample response from the api.
So far, I have tried below code in laravel with no success.
$data is received from api.
return response($data)
->withHeaders([
'Content-Type'=> 'application/pdf'
]);
The trick is to set the Content-Disposition header to force a download.
return response($data, 200, [
'Content-type' => 'application/pdf',
'Content-Disposition' => 'attachment; filename="api.pdf"',
]);
You could also replace attachment with inline to render the pdf in the browser (most modern browser support this).
Route::resource('get-users','api\UserController#get_users');
Route::resource('register','api\UserController#register');
My Api is giving me the response I checked it using POSTMAN but android developer is asking me that what should he sent in header for my API .
I am not getting that how to create a header I am using laravel and I have written functions in controller .
You should go through this part of the documentation: https://laravel.com/docs/5.6/responses#attaching-headers-to-responses
He maybe wants additional header in the response.
Your return value in api\UserController can be extended like this:
return response($content)
->withHeaders([
'Content-Type' => $type,
'X-Header-One' => 'Header Value',
'X-Header-Two' => 'Header Value',
]);
In my Laravel application I have a gallery for logged-in users with Amazon S3.
Now I download EVERY thumbnail and image in this way:
public function download($file_url){ // is safe, but slow...
$mime = \Storage::disk('s3')->getDriver()->getMimetype($file_url);
$size = \Storage::disk('s3')->getDriver()->getSize($file_url);
$response = [
'Content-Type' => $mime,
'Content-Length' => $size,
'Content-Description' => 'File Transfer',
'Content-Disposition' => "attachment; filename={$file_name}",
'Content-Transfer-Encoding' => 'binary',
];
return \Response::make(\Storage::disk('s3')->get($file_url), 200, $response);
}
This is safe (because I have a router with middleware('auth'), but is very server-intensive and slow.
Is it possible to download a file directly from Amazon:
only for (in my Laravel)-loggedin users (mayby with a temporery download link)?
OR only with a secure unique link?
You can use temporary URLs:
$url = Storage::disk('s3')->temporaryUrl(
'file.jpg', now()->addMinutes(5)
);
First param is the path on S3, second param is how long you want the URL to work for. Set this to a low value if you only want the URL to work for a single page load.
https://laravel.com/docs/5.6/filesystem (under Temporary URLs)
I am creating a web application on Codeigniter 3.2 which works with the Facebook Graph API. In order to make GET & POST HTTP requests, I need a curl library for Codeigniter. I have found Guzzle but I Don't know how to use Guzzle on Codeigniter.
Check this link:
https://github.com/rohitbh09/codeigniter-guzzle
$this->load->library('guzzle');
# guzzle client define
$client = new GuzzleHttp\Client();
#This url define speific Target for guzzle
$url = 'http://www.google.com';
#guzzle
try {
# guzzle post request example with form parameter
$response = $client->request( 'POST',
$url,
[ 'form_params'
=> [ 'processId' => '2' ]
]
);
#guzzle repose for future use
echo $response->getStatusCode(); // 200
echo $response->getReasonPhrase(); // OK
echo $response->getProtocolVersion(); // 1.1
echo $response->getBody();
} catch (GuzzleHttp\Exception\BadResponseException $e) {
#guzzle repose for future use
$response = $e->getResponse();
$responseBodyAsString = $response->getBody()->getContents();
print_r($responseBodyAsString);
}
You can integrate Guzzle into Codeigniter 3.x by following the following steps:
NOTE: I was doing this on Windows, should work on other platforms too.
Open the command terminal on your computer
Change directory to your Codeigniter app installation (i.e. my app is called MyCodeigniterApp)
cd C:\wamp64\www\MyCodeigniterApp
Install Composer in that directory by running the following command on the terminal
curl -sS https://getcomposer.org/installer | php
After installing Composer, we can now install Guzzle by running the following command on the terminal
composer require guzzlehttp/guzzle
if you encounter the following error while executing the above command
follow the recommendation in the error message.
Open composer.json located in your App root folder i.e. C:\wamp64\www\MyCodeigniterApp
then change
"mikey179/vfsStream": "1.1.*"
to
"mikey179/vfsstream": "1.1.*"
You can now re-run the command in step 4 to install Guzzle
After successful installation of Guzzle, you now need to make the following changes to the config.php file under the application/config directory
make the following changes under Composer auto-loading section and let the configuration be:
$config['composer_autoload'] = 'vendor/autoload.php';
The integration is complete, now you can use Guzzle in your controllers or models as below or by following the guides from Guzzle documentation on https://docs.guzzlephp.org/en/stable/
//Create guzzle http client
$client = new \GuzzleHttp\Client();
$res = $client->request('GET', 'https://api.github.com/user', [
'auth' => ['user', 'pass']
]);
echo $res->getStatusCode();
// "200"
echo $res->getHeader('content-type')[0];
// 'application/json; charset=utf8'
echo $res->getBody();
// {"type":"User"...'
DONE.......