Upload files

4 views
Skip to first unread message

Blue Sans douze

unread,
Feb 25, 2010, 10:10:28 AM2/25/10
to haxig...@googlegroups.com
Hello here !

I'm very new to haXigniter, and I love how the "getting started" tutorial is done. It's making the things really simple. That's a good work.

So, I have a question : How can I handle file upload with haXigniter ? Would be great to have it merged with the posted data :

public function create(posted : Hash<String>, files : Hash<File>)

or something like that. File object would contain the name, the mime type, and the temporary path to the file (apache does it like that, isn't it ?). Maybe some methods to. moveTo would be great.

For instant, if I have
<input type="file" name="blah" />
I would do :

public function create(posted : Hash<String>, files : Hash<File>)
{
    var blahFile = files.get("blah");
    if (blahFile.moveTo("imgs/uploaded/"))
    {
         //File uploaded succesfully !
    }
    trace(blahFile.name);
}

Ok, that does not exists for now. Is there a simple way to do that, or just to upload file, for now ?
I'm using mod_neko with apache.

Thanks for reading !
- Blue112

Andreas Söderlund

unread,
Feb 25, 2010, 10:41:08 AM2/25/10
to haxig...@googlegroups.com
Hi Blue, thanks for your interest in haxigniter! You're right, nothing exists for handling file uploads at the moment. However, the request handler system is built to be extended, so you can build your own RequestHandlerDecorator for handling file uploads:

If you look at http://github.com/ciscoheat/haxigniter/blob/master/haxigniter/server/request/HaxeRequestDecorator.hx you'll see one that's made for remote haxe requests. If you use that class as a base, the overridden method handleRequest() should look something like this:

public override function handleRequest(controller : Controller, url : ParsedUrl, method : String, getPostData : Hash<String>, requestData : Dynamic) : RequestResult
{
    var result = requestHandler.handleRequest(controller, url, method, getPostData, requestData);

    switch(result)
    {
        case noOutput:
          return result;

        case returnValue(value):
          return result;

        case methodCall(object, method, arguments):
          // --- Your code goes here, appending the resulting hash to the arguments array
          // depending on method and if uploaded file(s) exists ---

          return RequestResult.methodCall(object, method, arguments);
    }
}

To use the decorator, you wrap the requestHandler in your controller with the decorator:

class YourController implements haxigniter.server.Controller
{
  public var requestHandler : RequestHandler;
  public var contentHandler : ContentHandler;
  public var config : Config;

  public function new()
  {
    this.config = new config.Config();
    this.requestHandler = new FileUploadDecorator(new RestHandler(this.config));
  }
}

I saw that you got some help on the haxe list, I think you can use that code in the implementation. Please ask if you have more questions, and if your licensing allows it, send me the finished decorator so I can include it in the next release. :)


/Andreas

Blue112

unread,
Feb 25, 2010, 12:04:26 PM2/25/10
to haXigniter
Ohyeah !

It's working fine !
I should do the moveTo method, then it will be perfect. Actually it
only support one file upload, I should upgrade that feature.

So when I upload a file, and trace the files array (second parameters
of create) it says :

Upload.hx:62: {fl => { name => icon.png, tmpPath => /tmp/
fd9efab0bd25cd311d00d438b95f7abe }}

The file was currently icon.png, and when I go to /tmp/ I can see the
icon =)

So I'm kinda happy I get it to work that fast.
Tomorrow (or tonight) I'll complete it with moveTo and multi-file
import.

Thanks for the long-answer you give to me, it really helped me, I'd
never though I could do it by myself.

I'll submit it to you as soon as it's complete.
Thanks again.
Blue112.

On Feb 25, 4:41 pm, Andreas Söderlund <ciscoh...@gmail.com> wrote:
> Hi Blue, thanks for your interest in haxigniter! You're right, nothing
> exists for handling file uploads at the moment. However, the request handler
> system is built to be extended, so you can build your own
> RequestHandlerDecorator for handling file uploads:
>

> If you look athttp://github.com/ciscoheat/haxigniter/blob/master/haxigniter/server/...

Blue Sans douze

unread,
Mar 4, 2010, 10:55:10 AM3/4/10
to haXigniter
Ok, I've run some test, correct some bugs, and here I am !

Here is a full demo of the Decorator, with the decorator, integrity test and a controller to test it.
Please check the fixme into the decorator and tell me what do you think about.

Thanks !
Blue112.
uploadDemo.tar.gz

Andreas Söderlund

unread,
Mar 22, 2010, 2:02:12 PM3/22/10
to haxig...@googlegroups.com
Thanks for the code Blue! I'll integrate it in the framework as soon as I have a moment.

/Andreas
Reply all
Reply to author
Forward
0 new messages