Laravel nova action download a file causes : Failed - no file - php

I want to create a file and download it during a Laravel Action.
I use the following code:
public function handle(ActionFields $fields, Collection $models)
{
Storage::disk('local')->put('file.txt', 'example');
$url = Storage::disk('local')->url('file.txt');
return Action::download($url, 'file.txt');
}
This causes the following error :
Failed - no file
What am I doing wrong here?

Not sure where that error message comes from, but it may be the following problem. Calling return Action::download($url, 'file.txt') laravel nova will instruct the browser to download the file at the given url. As mentioned here this url will (depending on your config) probably be the relative url /storage/file.txt. So the browser will try to download http(s)://domain/storage/file.txt which might not be there. The docs mentioned above hold a solution for that.
If you wouldn't even want to create a file which would maybe need cleanup and protection if the data is sensitive, you could as well point the url to a route and do processing in a controller.

Related

Laravel - Force download with response()->download($fileLocation) not working

I'm creating a downloads functionality with Laravel. When the user clicks on the 'download' button, I initiate an ajax call to the controller which looks like this -
public function download(Resource $resource) {
// Force download of the file
$file_to_download = 'https://data.domain.com/downloads/' . $resource->file_name;
$temp_file_location = public_path('/tmp_files/' . $resource->file_name);
copy($file_to_download, $temp_file_location);
return response()->download($temp_file_location)->deleteFileAfterSend(true);
}
Chrome's inspector shows that the response gets populated with the contents of the file, but it won't trigger the actual download.
I've been trying to find an answer, but have had no success so far. Would really appreciate your help.
Thank you for your time.
You cannot download a file from an Ajax request because Javascript is unable to save files to your filesystem out of security concerns.
There are a few good packages out there like Jquery File Download
Or you can use a traditional GET request.

FileNotFoundException in Silex when returning a php image resource

i have an API route in my silex server that renders a php image resource and returns it via a BinaryFileResponse. I tried several response types and the BinaryFileResponse was the only one working properly. Nevertheless, every time i use this route to retrieve an image i get following error message in the logs:
silex: CRITICAL Symfony\Component\HttpFoundation\File\Exception\FileNotFoundException: The file "1" does not exist (uncaught exception) at /opt/pyriand3r/vendor/symfony/http-foundation/File/File.php line 41 | host='sv35' exception='error'
I believe this is, because i do not return an actual file but only a file resource not persisted in the filesystem.
Is there a way to erase this error without saving the image temporarly in the filesystem? Maybe another response type or something?
BinaryFileResponse is indeed intended for items in the filesystem. For your scenario, StreamedResponse would be appropriate.
In case StreamedResponse was among the response classes you unsuccessfully tried to get to work, you should post the failing code.

Laravel cannot find file in directory?

http://angulairapi.rohanchhabra.in/airports
This is a very simple route I have created in Laravel. It just takes a json file in the public directory, decodes it into an array and return the same json in response.
If you go the route (mentioned above), the error say "No such file or directory" but it exists in fact. It is working fine on my local machine. But when I pushed the same thing on my server, it is giving me this error.
http://gitlab.learningtechasia.com:8901/rohan0793/angulairapi.git
I have made the repository public so that everyone can have a look.
I have tested on my machine asset() is working for me and the path(public/airports.json) you have written reflect same error for me.
Laravel`s Helper function asset("file_name") generate a URL for an asset.
please check laravel`s helper function documentation for more detail
put this code in your routes.php and try again
<?php
Route::get('/airports', function(){
$airports = json_decode(file_get_contents(asset("airports.json")));
return Response::json($airports);
});
Route::get('/flights', function(){
$airports = json_decode(file_get_contents(asset("flights.json")));
return Response::json($airports);
});
EDIT
When you are working on local machine your url having word public i.e localhost/project-name/public/airports.json.
but when you deploy project on server it seems it remove public word from url, so what happing here, server finding airports.json at location http://angulairapi.rohanchhabra.in/public/airports.json but its not actually there its at location http://angulairapi.rohanchhabra.in/airports.json, so it is recommended to use laravel function(in this case asset()) to generate url/assets link.
An alternative to asset() and Anands answer is the helper function public_path(). It returns an absolute file path from the system point of view and not a URL.
$airports = json_decode(file_get_contents(public_path().DIRECTORY_SEPARATOR."airports.json")));
asset() should be used for URLs to files. URLs that you send to the client. You should work with public_path() for internal things, such as getting the content of a file.

Is there any option in Drive SDK for debugging?

I am having troubles with my PHP application which uses Drive SDK. I am trying to update a file, but all the time I receive 500 Internal Error message when I try to update file's contents.
I am looking for some way to debug the application. What would be most helpful for me is possibility to view how the entire request along with all headers look like. Is there any way to check it, or are there any other options for debugging?
Thank you a lot for your time.
I still didn't find any option for debugging- However, I found how I can view the requests done by the API client.
Open google-api-php-client/io/Google_REST.php file and find static public function execute(Google_HttpRequest $req) function.
There you will find this line:
$httpRequest = Google_Client::$io->makeRequest($req);
Right under it put the following code: var_dump($httpRequest);
During every request the client will do, you will get dump of it's request.
This question is still relevant but the accepted answer is very old. If you wish to view the HTTP requests and server responses in version three, the file you need to edit is /vendor/google/apiclient/src/Google/Http/REST.php. Locate the doExecute function and add print_r($request->getUri()); to the first line. Add print_r($response->getBody()->read(1024)); to the line just before the function returns to see the response body.

How to integrate a new file in a module in SugarCRM 7?

I added a new button in detailviews of a module, having the attribute onClick="genReg(name, type)".
I created this function in a .js file. This function (genReg(name, type)) send the variables to a PHP file, generateReg.php, using JSON.
The file is in *module_name* folder. In this PHP file I try to create a new object of modules Reglement (it's a custom module) and add new item to the database.
The problem is that it doesn't recognize the path to the module Reglement.
I included it as follows:
require_once('modules/Reglement/Reglement.php');
but got the following error:
Error: Failed opening required 'modules/Reglement/Reglement.php'
Paths to my files:
js: custom/module_name/js/generate.js
php: custom/module_name/generateReg.php
Which file shall I include in order to recognize all the path?
Yes, I did that error :D, calling directly. I change the url in js file like this http://localhost.net/index.php?entryPoint=generateReg and it works now. Thank a lot.
But I've got an other error. When I call this url I send POST data. In my file I have the POST data, but the function json_decode($_POST['data']) return nothing.
print_r($_POST['data']) -> return a valid json
json_decode($_POST['data']) return nothing
Do you know if Sugar do something that change the behaviour of json_decode?

Categories