开发者

Prepopulate file upload form in struts 2

I am working on a struts 2 web application where user can upload file through a file upload form, if the file already exists in database, a confirmation message should be displayed to overwrite the existing file or cancel the action.

On upload the database is queried for that file and if it exists the action results and set a flag in session which shows the jquery dialog box for user confirmation. I need to implement continue button in the dialog box which will set another flag to specify overwrite and call the same action again, this time the file will be uploaded. To implement this I need to prefill the file upload input box with previ开发者_开发技巧ous request parameter before calling the upload action.

Is there any other method to implement the above requirement?

Please let me know in case any details are required.

Thanks in advance.


I assume that you dont want to upload a file if a file with same filename exists in the database. If this is the case, why dont you check for the duplicity of filenames as soon as a file is selected to upload. This can be done in two ways

  1. load list of all the filenames along with the page and check the selected filename with this list
  2. Use ajax to send request to server for checking for duplicate filename when a file is selected

You can use the onchange event of the file element

For security reasons, you cannot mess with the value attribute of the file element programatically, otherwise you could fill-in any file from the user's system and submit the form


You can get user confirmation before sending the request by a checkbox asking whether to overwrite if the file exists. Then in your action do your IO stuff.

But if you still want to show the confirmation after sending the request I suggest you to use a custom interceptor that passes the request on both conditions of user confirmation and existence of the file. If the check didn't pass, you can return a custom result for confirmation part and resubmit the request. Just an idea.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜