locked
How to hit the url with customized parameters and download CSV. RRS feed

  • Question

  • User1052662409 posted

    I have a url (form) which doesn't require any credential it is accessible to everyone. There are some dropdownliats on that form.

    The user need to select drodpwnlist items and submit it, then there a a href comes with the download option. Now user can download CSV from there.

    I am not authorized to get / access that code.

    My requirement is that I need to make a form with same dropdownlist items. When I select the items from doropdownlist (from my form), that it should hit that url and download that csv for the user.

    Which approach I should follow.

    Please share your suggestions.

    Thanks

    Friday, January 31, 2020 4:09 AM

Answers

  • User475983607 posted

    demoninside9

    Now my requirement is that, I need to make a form where I can pass those items by typing in textbox (which are in dropdownlits) and hit the url with those items as parameters, then the that csv file should download.(on the same hit, which I have hit earlier when  I passed those items through textboxes as parameters. )

    As far as I can tell, you are asking how to submit an HTML form and use the form fields as querystring parameter to download a file from another action.   You have a lot of experience on this forum, so I'm thinking there something more to solving the this problem.    

    I'm guessing the link you are referencing to is some kind of handler or action that downloads the file.  You want to programmatically invoke the link?  Just craft a form that contains the inputs that you need.  Then craft an action with the same inputs and do a redirect to the file link.

            [HttpPost]
            public ActionResult DownloadFile(string selectValue, string textValue)
            {
                return Redirect($"/path/to/the/link?v1={selectValue}&v2={textValue}");
            }

    If you do not want to redirect you might be able to simply invoke the other Action method.

            [HttpPost]
            public ActionResult DownloadFile(string selectValue, string textValue)
            {
                return Reports(selectValue, textValue);
            }

    Other than that, I recommend meeting with your team to make sure you understand how the current application works.  Or share sample code on the forum that illustrates the problem you are trying to solve.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 31, 2020 2:15 PM

All replies

  • User409696431 posted

    If I understand you correctly, in the controller action that handles the submit, you currently check the value of the selection, construct the URL for the download, and display it in the view you return.   If so, modify that action to Response.Redirect to the URL you construct.

    Friday, January 31, 2020 6:43 AM
  • User1052662409 posted

    KathyW

    construct the URL for the download

    In the parent url the download button comes, when I click the submit button after selecting the items. There is no direct url to download.

    KathyW

    If so, modify that action to Response.Redirect to the URL you construct.

    Could you please share any example?

    Friday, January 31, 2020 6:56 AM
  • User409696431 posted

    "In the parent url the download button comes, when I click the submit button after selecting the items. There is no direct url to download."

    From your original post:

    "The user need to select drodpwnlist items and submit it, then there a a href comes with the download option. Now user can download CSV from there."

    Which is it?  You have the url, or you don't?  If you don't, what is the button code and what does it do?

    Friday, January 31, 2020 10:35 AM
  • User1052662409 posted

    Which is it?  You have the url, or you don't?  If you don't, what is the button code and what does it do?

    I have a form (URL) where I have some dropdownlists with a submit button. I did not developed it. For download data manually from this site, a user select items of dropdownlists and press a submit button.

    Once the user press submit button, there is a link comes to download the csv with the data based on the combination of dropdownlist selected items. 

    (even you can also download it's free for everyone).

    Now my requirement is that, I need to make a form where I can pass those items by typing in textbox (which are in dropdownlits) and hit the url with those items as parameters, then the that csv file should download.(on the same hit, which I have hit earlier when  I passed those items through textboxes as parameters. )

    I hope, I am able to make it clear now? If not please let me know.

    Thanks

    Friday, January 31, 2020 1:41 PM
  • User475983607 posted

    demoninside9

    Now my requirement is that, I need to make a form where I can pass those items by typing in textbox (which are in dropdownlits) and hit the url with those items as parameters, then the that csv file should download.(on the same hit, which I have hit earlier when  I passed those items through textboxes as parameters. )

    As far as I can tell, you are asking how to submit an HTML form and use the form fields as querystring parameter to download a file from another action.   You have a lot of experience on this forum, so I'm thinking there something more to solving the this problem.    

    I'm guessing the link you are referencing to is some kind of handler or action that downloads the file.  You want to programmatically invoke the link?  Just craft a form that contains the inputs that you need.  Then craft an action with the same inputs and do a redirect to the file link.

            [HttpPost]
            public ActionResult DownloadFile(string selectValue, string textValue)
            {
                return Redirect($"/path/to/the/link?v1={selectValue}&v2={textValue}");
            }

    If you do not want to redirect you might be able to simply invoke the other Action method.

            [HttpPost]
            public ActionResult DownloadFile(string selectValue, string textValue)
            {
                return Reports(selectValue, textValue);
            }

    Other than that, I recommend meeting with your team to make sure you understand how the current application works.  Or share sample code on the forum that illustrates the problem you are trying to solve.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 31, 2020 2:15 PM
  • User-474980206 posted

    this is called screen scraping. first see you can reverse engineer how the download url is produced from the select values. if you can not, then you will ned to screen scrape.

    your action will:

    • using httpclient, do a form post to the external url (use tracing to reverse engineer form fields). if the form uses anti-forgery, you will need to do a get first and parse out the token & cookie value
    • parse the response to get the download url
    • redirect to the download url. 

     

    Friday, January 31, 2020 4:44 PM
  • User409696431 posted

    "I have a form (URL) where I have some dropdownlists with a submit button. I did not developed it. For download data manually from this site, a user select items of dropdownlists and press a submit button."

    You must have access to the code.  What does the button do?  You can't replicate what it does without knowing what the button does now.  If it calls a URL, you need to know what the URL is and how it is formed in response to the dropdownlist.

    Friday, January 31, 2020 8:21 PM
  • User1052662409 posted

    mgebhard

    You have a lot of experience on this forum

    Sir, forum joining date doesn't make your experience. Your work does. How much you worked on newer technology. smile

    I just started learning MVC since last year (although the MVC came in existence long before 12-13 years), before I was working with web forms, static websites, SQL.

    So request you to please ignore my joining date on this form. But yes, I have worked a lot in dot net.

    mgebhard

    [HttpPost]
            public ActionResult DownloadFile(string selectValue, string textValue)
            {
                return Reports(selectValue, textValue);
            }

    I just find out by pressing F12. The url make the below html.

    <ul class="dropdown-menu" id="uldwnld" style="overflow: hidden; display: block;"><a href="#" data-toggle="dropdown" id="dwnld">
                                </a><li><a href="#" data-toggle="dropdown" id="dwnld"></a><a href="/wbes/Report/ExportFlowGateScheduleToPDF" id="PdfExport"> PDF</a></li>
                                <li><a href="/wbes/Report/ExportFlowGateScheduleToPDF" id="XlsExport"> Excel</a></li>
                                <li><a href="/wbes/Report/ExportFlowGateScheduleToPDF" id="CsvExport"> CSV</a></li>
                            </ul>

    I just want to click below href problematically 

    <li><a href="/wbes/Report/ExportFlowGateScheduleToPDF" id="XlsExport"> Excel</a></li>

    Thanks

    Monday, February 3, 2020 3:56 AM
  • User1052662409 posted

    You want to programmatically invoke the link?

    Yes, just want to click

    <li><a href="/wbes/Report/ExportFlowGateScheduleToPDF" id="XlsExport"> Excel</a></li>

    Monday, February 3, 2020 4:00 AM
  • User665608656 posted

    Hi demoninside9 ,

    According to your description, I suggest you can use bootstrap's dropdown framework and create a download action method in the controller to download the corresponding file.

    Specify the download method by adding the href attribute to each a tag, and carry the parameters by querystring.

    For details, please refer to the following code:

    Controller:

        public ActionResult Index()
            {
                return View();
            }
    
            [HttpGet]
            public ActionResult Download(string fileName)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "MyFiles/"; 
                byte[] fileBytes = System.IO.File.ReadAllBytes(path+fileName);
                return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            }

    Index.cshtml:

    @{
        ViewBag.Title = "Index";
        Layout = null;
    }
    <!DOCTYPE html>
    <html>
    <head>
        <title>Bootstrap Example</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css">
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.16.0/umd/popper.min.js"></script>
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js"></script>
    
    
    </head>
    <body>
    
        <div class="dropdown">
            <button class="btn btn-secondary dropdown-toggle" type="button" id="dropdownMenuButton" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                DownLoad Different Files
            </button>
            <div class="dropdown-menu" aria-labelledby="dropdownMenuButton">
                <a class="dropdown-item" href="/ControllerName/Download?fileName=Pdf.pdf">PDF</a>
                <a class="dropdown-item" href="/ControllerName/Download?fileName=Excel.xlsx">Excel</a>
                <a class="dropdown-item" href="/ControllerName/Download?fileName=Csv.csv">CSV</a>
            </div>
        </div>
    
    
    </body>
    </html>
    

    Here is the result of this work demo:

    Best Regards,

    YongQing.

    Monday, February 3, 2020 9:02 AM
  • User1052662409 posted

     public ActionResult Download(string fileName)
            {
                string path = AppDomain.CurrentDomain.BaseDirectory + "MyFiles/"; 
                byte[] fileBytes = System.IO.File.ReadAllBytes(path+fileName);
                return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
            }

    I did not host that application, I don't have path. I have only URL.

    Monday, February 3, 2020 9:25 AM
  • User-474980206 posted

    As the links are the same url, most likely there is some JavaScript that does the actual post back. Use the browsers network trace to see what is going on. 

    also if it currently a two step process, create then download, you may need to copy this. If you are adding code to an existing site you should show the code from the actions. Even if you can not change this code, you can call it from your own action. If it’s a different site, if CORS allows you can do it with JavaScript, if CORS not allowed you proxy the requests.

    Monday, February 3, 2020 3:17 PM