Blob - filename

157 views
Skip to first unread message

Ólafur Gauti Guðmundsson

unread,
Jan 4, 2011, 11:48:50 AM1/4/11
to play-framework
Hi everyone,
I've migrated my Play application from 1.0 to 1.1, and I have a
question regarding the switch from FileAttachment to Blob. It relates
to how I can access the filename of the Blob.

I have a class that stores a file (optionally), which can be of any
content type. This used to be a FileAttachment, but is now Blob,
declared in a class called Asset, as follows:

public class Asset ... {
...
@Column(name = "ev_file")
public Blob file;
...
}

Now, I both have a REST service, and a UI controller, which map my
object from HTTP parameters.

Both REST and the UI therefore expect a parameter called "asset.file"
which maps to the file. The HTML form submitting therefore has the
following field:

#{field 'asset.file'}
<label>Upload file:</label>
<input type="file" name="${field.name}" value="" class="$
{field.errorClass}" />
<span class="error">${field.error}</span>
#{/field}

The Asset object is mapped from HTTP params in the following way:

Binder.bind(asset, "asset", params.all());

I then have a controller serving up the file, from a URL similar to
the following: /asset/{assetId}/file/{filename}
The client wants and needs the filename there, so that the user does
not have to retype it if saving it locally.

Now, with the old FileAttachment, the filename was saved in the
FileAttachment object. But the Blob only stores the content type.

For those still reading, the question is:

Is there any way for me to access the submitted filename, assuming I
have access to the params object, request object (and all the declared
objects from Controller)?

Thanks for the help.
Regards,
OGG

Guillaume Bort

unread,
Jan 5, 2011, 7:56:04 AM1/5/11
to play-fr...@googlegroups.com
Well you are not forced to use auto-binding:

public static void upload(File file, String assetName) {
new Asset(file,assetName).save()
}

and in Asset:

public Asset(File file, String assetName) {
this.fileName = file.getName();
this.file.set(file, contentType);
}

2011/1/4 Ólafur Gauti Guðmundsson <oli....@gmail.com>:

> --
> You received this message because you are subscribed to the Google Groups "play-framework" group.
> To post to this group, send email to play-fr...@googlegroups.com.
> To unsubscribe from this group, send email to play-framewor...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,
write guillau...@gmail.com

Ólafur Gauti Guðmundsson

unread,
Jan 5, 2011, 10:40:32 AM1/5/11
to play-framework
Thanks for the reply.

I have been exploring this, and the only thing stopping me at the
moment is the fact that I would rather not have to change the REST
API.

The problem is that the file parameter is currently called
"asset.file", but as Java does not allow '.' in variable names, I
cannot declare a variable with this name.

Does Play support mapping of HTTP parameter names, for example:

public static void upload( @Param("asset.file") File file ) {
new Asset(file).save();
}

I've looked through the docs, but cannot find a way to achieve this.
Is there any way to override the default mapping of using the method
argument name to look up the HTTP parameter?

Thanks again,
OGG

On Jan 5, 12:56 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Well you are not forced to use auto-binding:
>
> public static void upload(File file, String assetName) {
>       new Asset(file,assetName).save()
>
> }
>
> and in Asset:
>
> public Asset(File file, String assetName) {
>       this.fileName = file.getName();
>       this.file.set(file, contentType);
>
> }
>
> 2011/1/4 Ólafur Gauti Guðmundsson <oli.ga...@gmail.com>:
> Guillaume Bort,http://guillaume.bort.fr
>
> For anything work-related, use g...@zenexity.fr; for everything else,
> write guillaume.b...@gmail.com

Guillaume Bort

unread,
Jan 5, 2011, 1:06:31 PM1/5/11
to play-fr...@googlegroups.com
Hum I see,

Then you should probably be able to write:

File f = params.get("asset.file", File.class);

2011/1/5 Ólafur Gauti Guðmundsson <oli....@gmail.com>:

> For more options, visit this group at http://groups.google.com/group/play-framework?hl=en.
>
>

--
Guillaume Bort, http://guillaume.bort.fr

For anything work-related, use g...@zenexity.fr; for everything else,

write guillau...@gmail.com

Guillaume Bort

unread,
Jan 5, 2011, 1:07:56 PM1/5/11
to play-fr...@googlegroups.com
Also you can keep using FileAttachment. It's now deprecated because a
few problems with transaction, but anyway if it works for you it is
ok.

Ólafur Gauti Guðmundsson

unread,
Jan 6, 2011, 10:33:53 AM1/6/11
to play-framework
Thanks a lot, Guillaume, this was exactly what I needed.
Problem solved.

Cheers,
OGG

On Jan 5, 6:06 pm, Guillaume Bort <guillaume.b...@gmail.com> wrote:
> Hum I see,
>
> Then you should probably be able to write:
>
> File f = params.get("asset.file", File.class);
>
> 2011/1/5 Ólafur Gauti Guðmundsson <oli.ga...@gmail.com>:
Reply all
Reply to author
Forward
0 new messages