Discussion:
Upload file and display its contents using AJAX
pureza
2012-12-04 16:51:38 UTC
Permalink
Hi,

I need to upload a file, parse it and display its contents inside a
textarea, all this using ajax.

At first I tried to use jquery-file-upload, and I was able to upload the
file to an IResource as explained at
http://wicketinaction.com/2012/11/uploading-files-to-wicket-iresource/.
However, I have no idea as to how to display the contents of the file inside
the textarea when the upload is finished.

Then I tried to use a plain FileUploadField component and I attached an
AjaxFormSubmitBehavior to it, but it seems that the model is always null and
I am unable to access the uploaded files.

Any help is appreciated.

Thanks!




--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Nick Pratt
2012-12-04 17:13:31 UTC
Permalink
Once the file is uploaded, set the contents of the IModel backing the
TextArea, and then add the Form(or TextArea) to the AjaxRequestTarget.
Post by pureza
Hi,
I need to upload a file, parse it and display its contents inside a
textarea, all this using ajax.
At first I tried to use jquery-file-upload, and I was able to upload the
file to an IResource as explained at
http://wicketinaction.com/2012/11/uploading-files-to-wicket-iresource/.
However, I have no idea as to how to display the contents of the file inside
the textarea when the upload is finished.
Then I tried to use a plain FileUploadField component and I attached an
AjaxFormSubmitBehavior to it, but it seems that the model is always null and
I am unable to access the uploaded files.
Any help is appreciated.
Thanks!
--
http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
pureza
2012-12-04 17:28:26 UTC
Permalink
Nick,

As I said on my previous email, I can't access the file contents within an
AjaxBehavior, because FileInputField.getModelObject() is always null (maybe
I'm not setting it up correctly when I instantiate the FileInputField?).



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473p4654477.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
pureza
2012-12-04 18:08:43 UTC
Permalink
Hi again,

I was able to get the desired result by putting an AjaxButton on the form
containing the <input type="file"> component. So, the user selects a file,
presses submit and everything works.

However, I'd prefer if the upload would start immediately after the user
selects a file. Can I get rid of the button?

Thanks.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473p4654478.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
pureza
2012-12-05 10:55:00 UTC
Permalink
I was able to get rid of the submit button, but I don't understand why it
works (maybe some wicket magic?).

I have a Form object and in the onSubmit() method I read the uploaded file
and store it in a model. I also added an AjaxFormSubmitBehavior to the form
that updates the textarea with the contents of the file via ajax.

The markup is simply:


This works fine (i.e. when I choose a file to upload the textarea is updated
with the file's contents), but I don't understand why. The form submission
does not refresh the whole page, but I was told that the only way I could do
this was using an iframe, which I'm not doing. Please note that I am also
not using any kind of upload library such as jquery-file-upload.

Do you have any idea as to why this works the way it does? I'm clueless...

Thanks!





--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473p4654508.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org
Martin Grigorov
2012-12-05 10:57:43 UTC
Permalink
Wicket uses an iframe internally for Ajax file uploads.
Post by pureza
I was able to get rid of the submit button, but I don't understand why it
works (maybe some wicket magic?).
I have a Form object and in the onSubmit() method I read the uploaded file
and store it in a model. I also added an AjaxFormSubmitBehavior to the form
that updates the textarea with the contents of the file via ajax.
This works fine (i.e. when I choose a file to upload the textarea is updated
with the file's contents), but I don't understand why. The form submission
does not refresh the whole page, but I was told that the only way I could do
this was using an iframe, which I'm not doing. Please note that I am also
not using any kind of upload library such as jquery-file-upload.
Do you have any idea as to why this works the way it does? I'm clueless...
Thanks!
--
http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473p4654508.html
Sent from the Users forum mailing list archive at Nabble.com.
---------------------------------------------------------------------
--
Martin Grigorov
jWeekend
Training, Consulting, Development
http://jWeekend.com <http://jweekend.com/>
pureza
2012-12-05 11:01:43 UTC
Permalink
Thanks Martin, I had just found
https://issues.apache.org/jira/browse/WICKET-2420 before reading your reply!

I guess that clears things.



--
View this message in context: http://apache-wicket.1842946.n4.nabble.com/Upload-file-and-display-its-contents-using-AJAX-tp4654473p4654510.html
Sent from the Users forum mailing list archive at Nabble.com.

---------------------------------------------------------------------
To unsubscribe, e-mail: users-***@wicket.apache.org
For additional commands, e-mail: users-***@wicket.apache.org

Loading...