Avatar of AlHal2
AlHal2
Flag for United Kingdom of Great Britain and Northern Ireland asked on

pressing download button in c#

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?
C#

Avatar of undefined
Last Comment
AlHal2

8/22/2022 - Mon
zephyr_hex (Megan)

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

ASKER
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?
zephyr_hex (Megan)

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
AlHal2

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

ASKER
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

ASKER CERTIFIED SOLUTION
zephyr_hex (Megan)

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AlHal2

ASKER
Thanks.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.