Lucas Infante
Lucas Infante

Reputation: 808

Plone 4 - Get url of a file in a plone.app.blob.field.FileField

I have a custom content type with 3 FileFields (plone.app.blob.field.FileField) and I want to get their url's, so i can put them on my custom view and people will be able to download these files. However, when using Clouseau to test and debug, I call :

context.getFirst_file().absolute_url()

Where getFirst_file() is the accessor to the first file (field called 'first_file').

The url returned is 'http://foo/.../eat.00001', where 'eat.00001' is the object of my custom type that contains the file fields...

The interesting thing is, if I call:

context.getFirst_file().getContentType()

It returns 'application/pdf', which is correct since it's a pdf file.

I'm pretty lost here, any help is appreciated. Thanks in advance!

Upvotes: 2

Views: 986

Answers (1)

Martijn Pieters
Martijn Pieters

Reputation: 1123900

File fields do not support a absolute_url method; instead, through acquisition you inherit the method from the object itself, hence the results you see. Moreover, calling getFirst_field() will return the actual downloadable contents of the field, not the field itself which could provide such information.

Instead, you should use the at_download script appended to the object URL, followed by the field id:

<a href="" tal:attributes="href string:${context/absolute_url}/at_download/first_file">First File</a>

You can also re-use the Archetypes widget for the field, by passing the field name to the widget method:

<metal:field use-macro="python:context.widget('first_field', mode='view')">
  First File
</metal:field>

This will display the file size, icon (if available), the filename and the file mime type.

In both these examples, I assumed the name of the field is 'first_field'.

Upvotes: 4

Related Questions