rails how to know when send_file done - ruby-on-rails

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.

Related

Web Scraping - Downloading Zip File

I'm trying to download a bunch of PDF files from a website that come bundled in a zip file with python. To download the zip file, I click a download button that makes a popup appear (I assume this is not important to the problem but I will include it for completeness). Chrome shows this when the download button is pressed and the popup appears:
I must then click the download button that's on the popup to actually begin the download. This is what follows:
I am quite confident that the first request is the only important one. If we look at the headers for this POST Request we see this:
All the POST data needed for this request can be scraped from the previous HTML page with the exception of the downloadedZipToken. This token is only generated/added to the html form after I click the download button on the popup and you can see that it's returned to me in the response header as a cookie.
So to summarize. In order to have a python script download the zip file for me I believe I have to mimic this POST request which I haven't been able to do because the zip token is not initially accessible. I apologize if this was confusing. Please let me know if more information is needed.
The downloadZipToken POST data that I wasn't able to find in my original question turns out to be a unix timestamp which makes a lot more sense as to why I couldn't find it in the HTML source. I assume it's generated by some JS script once the POST request is sent. To write my python code I just generated a unix timestamp with
timeStamp = math.ceil(time.time()*1000)

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.

How to download multiple files one after another

I have a ajax download functionality in my MVC webapp.
User can select a criteria and click on export button. Internally it will fetch data and return an Excel file. up to this functionality is working fine.
But the issue occurs, While one download process is running and now user changes the filter criteria and again click on export button. Now two download processes are running. Whichever process completes first will return file to download. Now the user can see Open, save, cancel option to download first file. As this stage when second download request is also completed and returns file to download. When I opens one file the another file download option is also lost.
Initially I thought it might because both the files are having same name. So I made changes to set unique file name for every request. But It still gives only single file to download.
Can anyone help me on this?
edited :
On other pages where I have two different types of files to download, the above functionality works successfully.
In none ajax requests, page can only be waiting for one response.
In order to solve that problem and wait for multiple responses you should use target attribute with value "new" as the following code depicts:
Your download Text
The above code makes each response to be downloaded in a new tab.

ActionScript FileReference upload onComplete

I am a complete beginner in Flash & Actionscript.
My pet project is this: To provide a www.imageshack.com like service where people could upload single images and later anyone can view it using the generated url.
So far I have gotten to upload an image using Flash and store it in a directory.
http://pixels.guygar.com/
You can check the uploaded image at:
http://pixels.guygar.com/warehouse/
The issue being, I was under the impression when the PHP file is called to store the image in the folder /warehouse the browser would automatically navigate to:
http://pixels.guygar.com/upload.php
Where I can process the image i.e. generate a unique file name and provide the user with a unique URL to later access the resource.
What is happening is the image gets uploaded by the PHP script but the browser page still stays the same page even when providing a new url in the PHP script.
So the question is how do I go about so that a new URL (image resource linked) is passed back to the flash so that onComplete is called I can navigate to image that was just loaded? Or other ways of doing such?
I welcome your perspectives on this issue and thank you for your guidance.
i would store all values that you need later on in a session on the server (don't forget to pass the sessionID to the upload-script via GET).
at the end of the PHP script you just return "ok" (or "ko" if sth went wrong) to flash and then (in the callback/listener) call/load a second PHP-script that's doing the rest ... and returns you an URL to a thumbnail or whatever you want to do.
hope this points you in the right direction ...
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/FileReference.html?filter_flash=cs5&filter_flashplayer=10.2&filter_air=2.6#event:uploadCompleteData
Shows how data can be returned to flash after an upload.

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 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.

Resources