Looking through it seems like some browsers (Firefox 3 at the least) have started working file uploads into their APIs so they can be used in AJAX, but I don't believe the cross-browser coverage for that is good enough yet.

I was reading into the IFrame solution when I happened upon a comment from someone saying you can return a 204 No Content HTTP Response to prevent a page refresh. Preventing a page refresh would be good enough for my page to continue otherwise functioning asynchronously while still allowing for file uploads.

I was able to implement this. I took the upload component out of the AJAX form and created a separate form for it in the block. This form calls an onSuccess method that handles the UploadedFile and then returns a NoContentStreamResponse I wrote up:

public class NoContentStreamResponse implements StreamResponse {

    @Override
    public String getContentType() {
        return "text/plain";
    }

    @Override
    public InputStream getStream() throws IOException {
        return new ByteArrayInputStream(new byte[]{});
    }

    @Override
    public void prepareResponse(Response resp) {
        resp.setStatus(204);
        resp.setContentLength(0);
    }

}

So while it is not exactly asynchronous, it does prevent page refresh (which for me was the main point anyway, avoiding the page render life-cycle), avoids using an i-frame and the extra code wiring to make that work, and more importantly it works. Thought I'd share it in case anyone else comes across a situation similar to mine.

On 03/11/2011 04:08 PM, Rich M wrote:
Oh... I did not realize that. Great reference, I'll see what I can figure out, thanks!

On 03/11/2011 03:17 PM, Thiago H. de Paula Figueiredo wrote:
File uploading doesn't work with AJAX without faking it using iframes or using Flash. It doesn't matter if you're using Tapestry or not, this is a limitation of AJAX itself. See this: http://stackoverflow.com/questions/543926/is-it-possible-to-use-ajax-to-do-file-upload.


On Fri, 11 Mar 2011 16:40:43 -0300, Rich M <rich...@moremagic.com> wrote:

Hi,

I've been working on Image file upload and display. With the help of a previous thread, I was able to get a fully working test component that could have a client upload a file from a form, and then have the file saved on the server and rendered back to the client in the browser.

I've moved on to integration of this concept into my actual application. I'm having an issue capturing the UploadedFile in any event handlers on a page where the form is within a Zone. The form is also within a Block that is delegated in case that factors in at all.

When the form is submitted, the UploadedFile object is null, and thus I cannot handle the object.

In my test component, the UploadedFile object resolves to a reference to the Image file uploaded by the client (had they uploaded one and no exception occurred). I did another sanity check by integrating the upload component into another form for the same Entity in my application that is not within a Zone or Block. The UploadedFile object was valid in this page as well after form submit, leaving me to think there is something about the Zone or Block that is interfering with the results I expected.

This is how I have been using the Upload component

Page:

@Property
private UploadedFile uploadedFile;

public void onSuccess(){
     if(uploadedFile == null){
         log.debug("Image file was not provided");
     }else{
         imageManager.saveImage(uploadedFile);
     }
}

TML:

<input t:type="upload" t:id="uploadedFile" />

Also since I'd had a couple threads recently where I had forgot to use the t:zone attribute properly, I tried out t:zone on the upload component to sanity check that as well, but that did not appear to change anything.

Is anyone familiar with what challenge(s) I may be facing here in getting the upload to work?

Thanks,
Rich

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscr...@tapestry.apache.org
For additional commands, e-mail: users-h...@tapestry.apache.org

Reply via email to