pressing download button in c#

AlHal2
AlHal2 used Ask the Experts™
on
Please see http://www.bmfbovespa.com.br/en_us/products/indices/broad-indices/brazil-broad-based-index-ibra-composition-index-portfolio.htm
At the bottom of the page there is a download button.
How do I press this download button from a C# program?
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2010

Commented:
You can't "press  a button" from server side code.  C# runs before the page is loaded to the client.  Interactions, like button pressing, occur on the client side.  If you want to press a button, you must use a client side language, like jQuery / JavaScript.

Author

Commented:
I've used a tiny bit of JavaScript in my own webpages.  This page is not my own.
How would I go about it.  Do you have any sample code?
Top Expert 2010

Commented:
This will click the link:

$("a[id$='Conteudo_Export_Test']").click();

Open in new window


You will want to wrap that inside a document ready function, so that it doesn't run until the DOM is loaded.

Here is a demo:  https://jsfiddle.net/zephyr_hex/rg90u61v/

HTML

<a href="" id="ctl00_contentPlaceHolderConteudo_Export_Test">Download</a>
<span></span>

Open in new window


jQuery

$(document).ready(function() {
   //test:  capture the click event
	$("a[id$='Conteudo_Export_Test']").on('click', function() {
  $('span').append('The link was clicked');
  });

  //click the Download link
	$("a[id$='Conteudo_Export_Test']").click();
});

Open in new window

Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

Author

Commented:
Thanks.  How can I schedule this to run at a particular time of day?

Author

Commented:
I've put this into a sample webpage.  How do I determine where the downloaded file goes?

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
   
   
        $(document).ready(function () {
            //test:  capture the click event
            $("a[id$='Conteudo_Export_Test']").on('click', function () {
                $('span').append('The link was clicked');
            });

            //click the Download link
            $("a[id$='Conteudo_Export_Test']").click();
        });
    
    
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    
    

        
        <a href="" id="ctl00_contentPlaceHolderConteudo_Export_Test">Download</a>
    
</asp:Content>

Open in new window

Top Expert 2010
Commented:
The file download is usually presented with HTTP headers and a MIME type that matches the file extension.  That triggers the browser to download the file.

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial