view examples/server.ur @ 1:f84d5e8aa992

Complete working example with required submit button click
author Adam Chlipala <adam@chlipala.net>
date Thu, 29 Dec 2011 15:26:46 -0500
parents 0c0f903d9440
children df095eecba63
line wrap: on
line source
sequence ids

table images : { Id : int, MimeType : string, Content : blob }
  PRIMARY KEY Id

fun main () =
    status <- source <xml/>;
    au <- AjaxUpload.render {OnBegin = set status <xml>Uploading</xml>,
                             OnSuccess = fn h =>
                                            let
                                                fun addImage () =
                                                    r <- AjaxUpload.claim h;
                                                    case r of
                                                        AjaxUpload.NotFound => return None
                                                      | AjaxUpload.Found r =>
                                                        id <- nextval ids;
                                                        dml (INSERT INTO images (Id, MimeType, Content)
                                                             VALUES ({[id]}, {[r.MimeType]}, {[r.Content]}));
                                                        return (Some id)
                                            in
                                                ido <- rpc (addImage ());
                                                case ido of
                                                    None => alert "Newly uploaded image not found!"
                                                  | Some id =>
                                                    let
                                                        fun image () =
                                                            r <- oneRow1 (SELECT images.MimeType, images.Content
                                                                          FROM images
                                                                          WHERE images.Id = {[id]});
                                                            returnBlob r.Content (blessMime r.MimeType)
                                                    in
                                                        set status <xml><img src={url (image ())}/></xml>
                                                    end
                                            end};
    return <xml><body>
      {au}
      <hr/>
      <dyn signal={signal status}/>
    </body></xml>