Start a dowload from external link in Rails - ruby-on-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).

Related

Ensuring file download from web page

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.

Open a link in web browser using flash swf

I am new to flash coding and didn't know much more.
I want to make a flash file that send URL to web browser to open particular link.
I've made such file it execute properly but after making swf file, it does not open the link.
Any one help me regarding this small problem.
Check out navigateToURL(): http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/flash/net/package.html#navigateToURL()
Note that it won't work unless it's triggered by a user action; i.e. a mouse click or a key press

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.

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.

Put Google Analytics code on a link?

How do you put google analytics code on a link, for example a link on your page to a download file. Is there a way to call the analytics function from a link?
Cheers.
You can track the click of the link using the _trackPageview function like so:
Download Now
The page view will be associated with whatever you pass to the _trackPageview function (/wherever/file.pdf in this case).
Google documentation is here.
One option for you would be to create a download page that redirects to the file to be downloaded and includes a link manually download if it doesn't automatically start.
Google would be used to track the page rather than the file.

Resources