Prepopulate file upload form in struts 2 - struts2

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 previous 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
load list of all the filenames along with the page and check the selected filename with this list
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.

Related

Rails upload excel file, validate and save

I am working on a rails engine that uploads a excel file, validates it and if there is no error than it will save it to database.
Now when ever a user mounts the engine and than go to the route provided by engine. He will have a form to upload the excel file. There are two buttons on page, i.e, upload and validate.
Once a user choose the file and when he click on upload i want that file only gets uploaded and don't get saved in db. Once i get the message the file is uploaded successfully, than i will validate the file. If it is a valid excel file with valid data than it will be saved into db. Now i am not getting how to go about it. I have seen this Railscasts video on uploading csv and excel file but here he is performing validation and save operation with import action but i want validation and save operation when user clicks on validate action. This Questions seems similar to my problem but i am not getting how do i access that uploaded file. I don't want that file to be saved in database. I mean when a user click on upload button that file gets only uploaded not saved. Than i will validate that file and save it's content to db.
This may seem very easy and simple questions for some experts but i am very new to rails and i am not sure how to go about it.
Someone please help me with a sample code, so that i can understand the workflow. Also note that both upload and validate actions are on same page. So when a file gets uploaded it needs to be stored somewhere temporarily, this is the first problem i am facing. I can do all the task if someone can tell me workflow with a sample code about uploading excel file. I am only having problem here that as both upload and validate action are on same page, so after upload request it needs to be on that page so that i can validate that file.
Any help would be appreciated, I am very beginner at rails and really confused here.
Two options:
Write code to upload the file and save to DB with a validated column set to false. Then the 'validate' button will locate the unvalidated file, validate it and set validated to true. You could have a periodic job deleting unvalidated files of a certain age. If you do this, use a helper gem like Paperclip.
Forego file upload frameworks and just manually save uploaded files to Tempfile.new 'spreadsheet'. This guide takes you through how to do that. Save that filename to session and use it to validate at a later point. When you're finally ready to persist to DB, again, consider using a helper gem.

Processing a long running process (uploading large Excel files); Ruby on Rails 4.0

I have a create action in a controller that is in charged of uploading Excel files. Some of these Excel files take a long time to process. Once the Excel file is validated and the content of the file is saved in a table, the user is redirected to another view where they see a success flash notification.
I would like to alter this process but honest to gosh I would love some guidance or perhaps a link to something similar.
1) I am trying to make it so, once the create button is selected and the validations pass, the user is redirected elsewhere and once the file upload is completed, the user will be notified regardless on where they are in the application.
2)While an excel file is being processed, the create option is not available to any other user. (Currently, if the file is being processed, it stays in the new view and if you keep clicking on the create button, it processes the file over again prolonging the uploading process)
Thanks in advance for the help!
From my point of view you should do following steps:
1) Create a special storage for states (global place where information about file upload status is stored). I propose to use key-value storage such as redis or memcached
2) When user upload file you save it to the disk, queue background job (resque or delayed_job) and set flag file_is_processing to true
3) In your views disable button if file_is_processing flag is set.
4) In background job you validate file and if it is not valid set a flag in storage not_valid_file & remove file_is_processing flag
5) Have before action where you check this state and if file is not valid redirect user to file upload page with notice & remove not_valid_file flag.
6) If file is valid you parse file then remove file_is_processing flag & set file_is_processed
7) In before action redirect user to success page, show notice & remove file_is_processed
Seems to be easiest way for me.

Kendo UI upload control remove firing after save when multiple set to false

I am using the Kendo Upload control and need to allow the user to only upload one file at a time. I set the multiple option to false on the client side and this works. However, if a user uploads a file and then immediately tried to upload a second file, the removeUrl is called first which gets passed a string array of file names. Here is the problem, when I save a file I am renaming the file to a name using guid values and creating a database table entry to associate the file with the user. I need a way to reset the filename to the guid after the file is uploaded and its name is displayed? Is there a way to do this?
Try utilizing the Files configuration option. With that, you should be able to reset the displayed file names to any name you choose.

Can I somehow reinstate the file name on a 'file' type input if the page post fails?

Often some validation factor with nothing to with the file will cause a server side validation error, and reload the page. The previously chosen filename is always missing, and the poor user has to select the file again. This is especially frustrating when they fix the other error and click Submit again, thinking all will be all right.
Can I not somehow persist the file name in my model and set it on the 'file' input when the page loads?
Every form submission requires complete file upload, so to not make user poor, you should store file on server even when validation fails. When you do it, you have list of already uploaded files on the server ready for next, proper form submission. You can also present box with already uploaded files in view.
For security reasons, manipulating file upload field is not possible.

rails how to know when send_file done

As it takes some time to prepare the content of the data to be downloaded, I want to show a message "Preparing file to download" when the user submits the request
Then when the file is ready, I use send_file to send the data
Once it's done, I need to clear the message
Thanks a lot
Here is what I would do.
1. Create a prepare_file action
First, I would create an action whose job would just be to create the file and rather than rendering HTML, it would render a JSON object with the name of the file that was created.
2. Use AJAX to call the prepare_file action
On the client, when the user clicks to download the file, you display the message, "Preparing download..." and just do an AJAX request to that action. The response you'll get back via AJAX is the name of the file created.
3. Redirect to the file download
Finally, you can hide the preparing download message and redirect the browser to the file download via JavaScript with the name of the file that was created. You would use send_file in this action.
I know that, in the question, you also wanted to be able to display to the user a message when the file is downloading and another message when it is finished. However, this isn't possible unless you write your own client-side download manager. The browser handles file downloads entirely and the user will see in the browser that the file is downloading and what the progress is. So, I understand where you're coming from, but you shouldn't feel like the user isn't being informed of what's happening.
At least with this solution, you're displaying a message to them when the file is being prepared and then once that message disappears, they'll get the download file dialog from the browser.
If you need help with actual code samples of how to do this, let me know.

Resources