Ensuring file download from web page - ruby-on-rails

We have a website where users pay for download access to a given document, and so we need to ensure the download can be successfully executed.
Our download action looks like this:
send_data generate_document(),
filename: "yourfile.pdf",
type: "application/pdf",
disposition: "attachment"
...and it works perfectly fine on all the browsers I've tested.
However, I often see download pages with prompts similar to the following:
"If your file doesn't download automatically, click this link."
What's the reason for those prompts? Why would downloads fail initially, and what happens differently when users click the link?

I think most of the cases it's for very old browsers (at least that's what I was told by my colleague). Also some websites implement this using JS, those who don't have it enabled for some reason, can't see the download.
When user clicks a link, he explicitly directs browser to that location, such actions usually should work everywhere.

Related

Start a dowload from external link in Rails

I'm trying to write code in my controller download to start a download from some external url say www.abc.com/foo.mp4. So far I have built it this way:
def download
redirect_to 'www.abc.com/foo.mp4'
end
It works but the problem is that it opens a video in the same tab. What I want is that it should start a download in the browser of this video.
I searched about it in forums but couldn't find any solution. All I found was a way to download the video first using the URL and then provide it as download but that's not what I want. I want the download to start directly from the external URL.
I think this might help you: Download from a link with HTML5
In your case it would be:
download me
That way, from the same webpage you are, when the user clicks on it, it will start the download and you don't go throught the controller, but in case you want to go throught the controller "download" and then just render a page with some kind of information saying that is downloading or something else, then put that link I wrote above somewhere in that page (and if you want, you can give it a class name and with css hide that class so that link is not visible but it is there) and at the end a Javascript function that calls itself when it is loaded and performs the action of clicking to that link:
(function(){
$('#whateverIdYouGiveToTheHtmlElement').click();
})();
I should clarify that $('#whateverIdYouGiveToTheLink').click(); is jQuery and not pure Javascript, but since you are using Rails I assume the project has jQuery (by default Rails comes with jQuery).

Display docx or ppt in browser with ruby

I'm currently working on a intranet webapp for a company.
I've created it so the administrators of the site are able to upload files
(.docx, .pdf, .xlsx, .ppt etc) up to the webapp, to provide easier access
to documents for the employees. It works very well, however my client wasn't
too fond of having to download the files, and wanted it to pop up in the browser,
or open up the file-spesific program instead of download.
I was playing with some ideas:
1. Somehow parse the files to JSON at upload, and then show the content in browser with html.
2. Generate a pdf from the uploaded file (which automatically launches in the browser).
3. Somehow use a previewer to show the filecontent in the browser
4. Clients computer launches the uploaded file automatically on download, however I think this is a bit more tricky...
What would be the best and most time-efficient way to go about this?
It feels like what you actually want/need is a javascript document viewer (only) such as http://viewerjs.org/.

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.

browser downloads rails response as a file

I have been fiddling with rails apps for a while and I sometimes see this behavior on Safari (could be on other browsers as well, not sure) where the browser downloads the response as a file instead of rendering it in the browser. I wonder why that is.
The last time it happened made me decide to ask on stack overflow. My current setup is a very basic rails app that implements devise and I get a file named 'sign_in.admin' with just the following content:
You need to sign in or sign up before continuing.
I know that string comes from the devise locale and is supposed to be rendered with a layout, but the browser just downloads it as a file as if I chose to 'Download linked file as...' It's happened to me before where instead of rails routing me somewhere it downloads a file. Can somebody explain why my browser sometimes does that? I have all the latest updates.

Open VCF file after download complete in android from Browser

I have generated a Vcard in rails and set it to download by using the following code
send_data Card.generate_vcard(asset).to_s, :filename => "contact.vcf", :type => "text/x-vcard; charset=utf-8",:disposition => 'attachment'
When someone goes to URL, the Vcard gets downloaded. But I also require that it automatically gets opened in phone's contacts app - or at least present an "Open with" option.
I followed this link http://lrvick.net/blog/accessibly_importing_vcards_from_qr_codes/ and went for second approach mentioned there, set the proper headers as well - but it is not work for me.
Thank you for Your Help
There is no way to do that, the file will just get downloaded.
You should also add a filename, such as
Content-Disposition: attachment; filename="name_surname.vcf"
I noticed some browsers would not accept it as vCard otherwise.
Besides, this encourages the user to click on the downloaded file (and open it).
For Apple:
iOS now accepts vCard, for older version (> 4) you can cheat and embed the .vcf in a calendar .ics. See this tutorial for more details
For oldest version (<3) you must send the vCard by email..
For both, the best solution should would a URI-Scheme that would let us add a contact. But that is not available yet, even though I know iOS has some specific tools for that.
We're waiting for a better HTML5 there :).

Resources