Tag Archives: programming

File Upload using Perl and DANCER

codeImage
One of the most common functions in a web application is File Upload. The following is a working code that can be used to this end. The prerequisites are only Perl (obviously) and the DANCER framework. Right! Here goes:
post '/upload/:file' => sub {
  my $upload_dir = "/home/fpsom/myApp/UPLOADS";
  my $filename = params->{file};
  my $uploadedFile = upload('file_input_foo');
  $uploadedFile->copy_to("$upload_dir/$filename");
  debug "My Log 1: " . params->{file};
  debug "My Log 2: " . ref($uploadedFile);
};
This should be copied in the myApp/lib/myApp.pm file (check for the corresponding file in your app)
In order to check the functionality of this code, I’ve used cURL as follows, where testUploadFile is an existing file:
curl -i -F file_input_foo=@testUploadFile http://localhost:3000/upload/testUploadFile
The output on the “development dance floor” should be as follows:
[10269]  core @0.000119> request: POST /upload/testUploadFile from 127.0.0.1 in /usr/local/share/perl/5.14.2/Dancer/Handler.pm l. 56
[10269]  core @0.000413> [hit #4]Trying to match 'POST /upload/testUploadFile' against /^\/upload\/([^\/]+)$/ (generated from '/upload/:file') in /usr/local/share/perl/5.14.2/Dancer/Route.pm l. 84
[10269]  core @0.000530> [hit #4]  --> got 1 in /usr/local/share/perl/5.14.2/Dancer/Route.pm l. 102
[10269]  core @0.000627> [hit #4]  --> named tokens are: file in /usr/local/share/perl/5.14.2/Dancer/Route.pm l. 130
[10269] debug @0.001051> [hit #4]My Log 1: testUploadFile in /home/fpsom/myApp/lib/myApp.pm l. 21
[10269] debug @0.001145> [hit #4]My Log 2: Dancer::Request::Upload in /home/fpsom/myApp/lib/myApp.pm l. 22
[10269]  core @0.001446> [hit #4]response: 200 in /usr/local/share/perl/5.14.2/Dancer/Handler.pm l. 179
And that is that! Hope it helps.

Perl DANCER framework

After quite some time, I had to go back to writing web-services. I can’t say I was really looking forward to it, but don’t get me wrong; I love writing code and I feel quite confident at it. On the other hand, my memories in writing web-services aren’t the most comforting ones. Admittedly, last time was in 2008 and was writing in Java at that time, so I had to juggle with a lot of things to get them working just right, but still…
perlDancer
Anyway, my project now is in Perl so I had to look a little bit around for any existing frameworks that might make my life easier, when I came up with the DANCER framework. And it was a brand new day…
That was two days ago. In this time I’ve been able to setup almost everything writing only a fraction of the code I used to. Now I am working on the particulars of the application behind the web-service. The setup of the service is staightforward; the DANCER dev team actually provides a tool to help along the way. But (as in every transition between languages) there are several bumps along the road after that. So, in order to help anyone interested, I’ll share any “recipes” that I come up along the way.
Good coding to all!