I added mp3 files to the /public folder and I found a bit issue.
In my view I added the links:
= link_to "Interview 1", "/interview_01.mp3"
When I see the link from the page, everything appears normal until I click on it. The browser hangs on for a minute or more, the tile changes to "undefined" and my Google Chrome freezes.
What's wrong with it? How can I make it so people can just download the file?
Edit: More context for my answer since someone down voted me
When you link to an mp3 file, most modern browsers will attempt to play that file directly in the browser. Your browser is likely freezing because whatever plugin your browser is using to begin playing the file is broken. My guess would be you are running windows and it is attempting to use a quicktime plugin or something along those lines.
If you don't want the browser to play the file and you actually want the file to begin downloading, you need to add a few extra headers to the HTTP response. The most important being:
Content-Disposition: attachment; filename="filenamehere.mp3"
My original answer provides instructions for accomplishing this. If this is not what you were hoping to achieve then you can disregard it.
If you want the file embedded directly into the page with the new HTML5 audio tag, you can follow the advice given in the comment to your question by #Alen.
Original answer:
You should try sending the file from a controller action using the #send_file controller helper method.
= link_to "Inteview 1", :controller => 'files', :action => 'send_file', :filename => 'interview_01.mp3'
Then
class FilesController
def send_file
file_path = "#{Rails.root}/public/#{params[:filename]}"
send_file file_path, :filename => params[:filename], :disposition => 'attachment'
end
end
Reference Rails Api: http://api.rubyonrails.org/classes/ActionController/DataStreaming.html#method-i-send_file
The send_file method sets the HTTP headers correctly to tell the browser to download the file instead of attempting to render it.
Related
In my rails application i am using action mailer to send mails to users. I want to give some bonus points to user if he opens the mail. For that i want to get notification when the email got opened in user end. Is there any idea to achieve this?
I tried as below.
I added one image tag in the mail template like below:
<%= image_tag tracking_image_url(1)%>
And in controller
def image
file_path=File.dirname(::Rails.root.join('public','assets','customer_ads','2','medium','medium')) + "/Huawei.jpg"
send_file file_path, :type => 'image/jpeg'
end
I am not getting any notification from this. It's just downloading the image.
I achieved through ahoy email gem.
I want to capture screenshot of the browser URL section.
browser.screenshot.save ('tdbank.png')
It will save the entire page of internal part of the browser, but I want to capture the URL header part of the browser. Any suggestion?
Sometime, URL is saying http or https. I want to capture this in screenshot and archive it. I know I could get it through,
url = browser.url
then do some comparison. I need this for legal purpose and it should be done by taking a screenshot.
thanks in advance.
If you're on windows, you could use the win32screenshot gem. For example:
require 'watir-webdriver'
require 'win32/screenshot'
b = Watir::Browser.new # using firefox as default browser
b.goto('http://www.example.org')
Win32::Screenshot::Take.of(:window, :title => /Firefox/).write("image.bmp")
b.close
Example:
require 'sinatra'
get '/somekey' do
headers('Content-Type' => "image/jpeg")
["http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg", "http://img.brothersoft.com/screenshots/softimage/j/jpeg_converter-4567-1.jpeg"].sample
end
I want to respond with an image that is not hosted on my server.
How would I get about this?
Note:
The link to the image is not secret (as it is hosted on S3). It is for a site that generates identicons.
Checkout http://identico.in/[insert_any_key_here]. The reason is I wanted the server to do a look up, if the image already exists on S3, use that one, if not, generate one and then upload it to s3.
Note:
If I did:
require "open-uri"
open ["http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg", "http://img.brothersoft.com/screenshots/softimage/j/jpeg_converter-4567-1.jpeg"].sample
Then it works, however, I feel that this might be a lot slower because my server first has to download the image and open it and then the user has to download the image from my server.
yes, if you want to send from your server, you need to have it on your server before sending. So most of the time you need to use send_file open('link') and be a proxy from storage server and a client.
require 'sinatra'
require 'open-uri'
get '/' do
send_file open('http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg'),
type: 'image/jpeg',
disposition: 'inline'
end
But if a link is not a secret, you can just render some javascript and it will open image in browser.
require 'sinatra'
get '/' do
"<script>document.location = 'http://upload.wikimedia.org/wikipedia/commons/2/23/Lake_mapourika_NZ.jpeg'</script>
end
i am trying to upload a file to box.com with their v2 api.
i am able to successfully upload a file with curl, but cannot upload a file from my rails application. i am passing my upload function the correct folder id and file is a tempfile object created by a form upload in my app.
here is the successful curl command
curl https://upload.box.com/api/2.0/files/data -H "Authorization: BoxAuth api_key=API_KEY&auth_token=TOKEN" -F contract=#test.png -F folder_id=387656851 -ssl3
and here is my ruby code
class BoxApi
require 'httmultiparty'
include HTTMultiParty
ssl_version :SSLv3
def initialize
#key = API_KEY
#token = TOKEN
end
def upload_file(folder_id,file,filename,content_type)
File.open(file) do |open_file|
response = self.class.post('https://upload.box.com/2.0/files/data', :query => {
:file => open_file,
:folder_id => folder_id
}, :headers => {'Authorization' => "BoxAuth api_key=#{#key}&auth_token=#{#token}"})
p response
end
end
i get an html page back from box with this text
It appears that your firewall may be blocking Box or you are encountering an error.Please contact your IT administrator to configure your firewall to recognize all sub-domains of .box.com, .box.com and .boxcdn.net. The ports that should be opened for these domains are 80 and 443.If that does not resolve the issue, then please submit a support ticket at https://www.box.com/help.
any ideas why the curl command would be working but not the ruby code?
Despite from being late, this could be useful for people who came across this question.
There is a gem ruby-box to use with Box service at the 2.0 version of their API.
This works properly for me
require 'httmultiparty'
class SomeClient
include HTTMultiParty
base_uri 'https://api.box.com/2.0'
end
response = SomeClient.post('/files/data',
:headers => { 'authorization' => 'BoxAuth api_key={YOUR API KEY}&auth_token={YOUR TOKEN' },
:body => { :folder_id => '0', :somefile => File.new('large.jpeg')}
)
I would try to verify that
You can make non-upload API calls (i.e. GET /folders/0)
If not, check your firewall settings.
Sean already covered this in his answer but I'll highlight it explicitly. We had some issues using the https://upload.box.com URL which is no longer recommended by box. I'd recommend trying the https://api.box.com/2.0 URL and seeing if that it changes your results.
Worst case I'd try capturing my packets using a packet analyzer like wireshark and looking for differences between the two cases.
I am experimenting with using rspec and watir to do some tdd and have come across a problem I can't seem to get past. I want to have watir click a link (target="_blank") and then get the url of the newly loaded page. Watir clicks the link but when I attempt to get the url I receive the old url not the current. Watir docs seem to indicate that the Browser url method will return the current url. I found a blog post that seems to solve this issue by having Watir execute some javascript to get the current url but this isn't working for me. Is there anyway to get the current url from a link click with Watir?
<!-- the html -->
LinkedIn
#The rspec code
it "should load LinkedIn" do
browser.link(:href => "http://www.linkedin.com").click
browser.url.should == "http://www.linkedin.com"
end
The target will load the link in a new browser window, therefore you need to switch to that window to assert the url:
it "should load LinkedIn" do
browser.link(:href => "http://www.linkedin.com").click
browser.window(:title => /.*LinkedIn.*/).use do
browser.url.should == "http://www.linkedin.com"
end
end
See: http://watirwebdriver.com/browser-popups/ for more examples