Implement a way to get an absolute URL to a file.
Yes this is obsoletely required. vich_uploader_asset functions this option. Is there anyway to do this !
:+1:
In a controller, you can just prepend this string:
$baseUrl = $this->getRequest()->getScheme() . '://' . $this->getRequest()->getHttpHost() . $this->getRequest()->getBasePath();
If you need it in a template, it's just the same, using app.request of course
So how to use in twig template
{{ vich_uploader_asset(entity, 'file_name') }}
Can i prepend app.request with above. I tried that throws error
try this :
{{ app.request.basePath }} {{ vich_uploader_asset(entity, 'file_name') }}
As I told before, it's:
{{ app.request.scheme }}://{{ app.request.httpHost ~ app.request.basePath ~ vich_uploader_asset(entity, 'file_name') }}
even easier, like this
{{ app.request.getUriForPath(vich_uploader_asset(entity, 'file_name')) }}
+1 for @patrickli
I think vich_uploader_asset should generate url with base path. @dustin10 can you fix it please?
I no longer maintain this bundle so I will not be fixing it. It should probably abstracted out through the StorageInterface because if you are using Gaufrette or something else then your images may be on Amazon servers or something and not in your application domain.
using request object like {{ app.request.scheme }}://{{ app.request.httpHost }} is not a general solution, because request is not available in all cases (eg. console commands).
I suppose we can add / expose a parameter for each mapping, smth like "base_url", and use it to generate absolute url to file in resolveUri method:
/**
* {@inheritDoc}
*/
public function resolveUri($obj, $mapping, $className = null, $absolute = false)
{
//...
$path = $mapping->getUriPrefix() . '/' . $mapping->getUploadDir($obj) . $filename;
if ($absolute) {
$path = $mapping->getBaseUrl() . $path;
}
return $path;
}
# app/config/config.yml
vich_uploader:
db_driver: orm
mappings:
product_image:
base_url: http://example.com
uri_prefix: /images/products
upload_destination: %kernel.root_dir%/../web/images/products
@mgoria if you need to use request scheme or http host in command line, you should configure it. See How to Generate URLs and Send Emails from the Console Define base urls for every mapping is not a good idea
Right now you can define an absolute uri_prefix:
vich_uploader:
db_driver: orm
mappings:
product_image:
uri_prefix: http://example.com/images/products
upload_destination: %kernel.root_dir%/../web/images/products
Not sure if we need something more complex as it even covers the case of Amazon S3
@patrickli thanks, {{ app.request.getUriForPath(vich_uploader_asset(entity, 'file_name')) }} this is perfect
# app/config/config.yml
framework:
assets:
base_urls: ['http://example.com']
{# template.twig.html #}
{{ asset(vich_uploader_asset(event, 'fileLogo')) }}
That gives me http://example.com/path/to/asset/0123_456.jpg
what about {{ asset(vich_uploader_asset(image, 'imageFile')) }} ...this work for me ;-)
@RafalJaworski Thanks. Works like a charm for me.
Hello, for me, when I use this line for generate fiel URL download, if I click on the URL, I've a new tab with what my file contains ( txt, jpg, but if it's a .xsl by example, I've an error).
How can I force the download please ?
Hello, for me, when I use this line for generate fiel URL download, if I click on the URL, I've a new tab with what my file contains ( txt, jpg, but if it's a .xsl by example, I've an error).
How can I force the download please ?
<a download href="...">
Bonjour, quand je me sers de cette ligne pour g茅n茅rer le t茅l茅chargement d鈥橴RL de champ, si je clique sur l鈥橴RL, j鈥檃i un nouvel onglet avec le contenu de mon fichier (txt, jpg, mais si c鈥檈st un .xsl par exemple, je v une erreur).
Comment puis-je forcer le t茅l茅chargement s'il vous pla卯t?
<a download href="...">
You're the best, thanks !
Hello, for me, when I use this line for generate fiel URL download, if I click on the URL, I've a new tab with what my file contains ( txt, jpg, but if it's a .xsl by example, I've an error).
How can I force the download please ?
<a download href="...">
Ok just a question. All is good. But I can't download .php files. For all other types of files it works. A solution ?
For others who had the problem of generating URLs for a file, I modified my own topic to put the changes I made : https://github.com/dustin10/VichUploaderBundle/issues/976
Most helpful comment
even easier, like this
{{ app.request.getUriForPath(vich_uploader_asset(entity, 'file_name')) }}