locked
How to download a csv file from a url RRS feed

  • Question

  • User-454825017 posted

    see my code is working to download csv file from a url

    function downloadFile(fileName, urlData) {
    
        var aLink = document.createElement('a');
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click");
        aLink.download = fileName;
        aLink.href = urlData;
        aLink.click(evt);
    }
    downloadFile('INSTHOLDING.csv', 'https://test.com/targeting/Export/test.csv');

    in the above url file name is test.csv but i want when this file will be downloading in client pc then name should be inst.csv. how it would be possible ?

    Friday, July 3, 2020 1:51 PM

Answers

  • User288213138 posted

    Hi TDP,

    if i change the file name in url then how js can download the file ?

    Do you mean you only need to set the file name in client pc?

    If so, you can try this code.

    function download(filename, urldata) {
                var a = document.createElement("a");
                a.href = urldata;
                a.setAttribute("download", filename);
                a.click();
            }
    
    download("helloWorld.txt", "data:text/html,HelloWorld!");

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 8, 2020 2:49 AM

All replies

  • User348806598 posted

    Try-

    function downloadFile(fileName, urlData) {
    
        var aLink = document.createElement('a');
        var evt = document.createEvent("HTMLEvents");
        evt.initEvent("click");
        aLink.href = urlData;
        aLink.download="inst.csv"
        aLink.click(evt);
    }
    Friday, July 3, 2020 2:05 PM
  • User348806598 posted

    Hi,

    Realized that this may not work in all the cases. Here is the explanation(https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a, search for dowload).

    If you have server-side access, you can name the file whatever you want.

    Friday, July 3, 2020 3:03 PM
  • User-454825017 posted

    yes i did it aLink.download="inst.csv"

    but still file name not changed. there must be some other workaround. 

    Friday, July 3, 2020 5:08 PM
  • User-474980206 posted

    the download attributes filename will only be honored if its the same origin. be sure to use relative path. also the browser must support it, did you try chrome?

    Saturday, July 4, 2020 4:08 PM
  • User-454825017 posted

    yes i tried chrome. i am downloading file from a different web site. not same origin. can you please tell me any other work around by which i can download that csv file from other web site url using JavaScript but i can give a different name to file during downloading the file in client pc. is it not possible ?

    Sunday, July 5, 2020 5:28 PM
  • User475983607 posted

    yes i tried chrome. i am downloading file from a different web site. not same origin. can you please tell me any other work around by which i can download that csv file from other web site url using JavaScript but i can give a different name to file during downloading the file in client pc. is it not possible ?

    Write server side code to download the file.  Your JavaScript application calls your C# code to download the file.

    Sunday, July 5, 2020 5:35 PM
  • User-454825017 posted

    yes i think that would be last option. thank you.

    Sunday, July 5, 2020 5:52 PM
  • User288213138 posted

    Hi TDP,

    in the above url file name is test.csv but i want when this file will be downloading in client pc then name should be inst.csv. how it would be possible ?
    'https://test.com/targeting/Export/test.csv'

    You can use replac() method to modify urlData.

    var str = 'https://test.com/targeting/Export/test.csv';
    
    var res = str.replace('test.csv','inst.csv');

    Best regards,

    sam

    Monday, July 6, 2020 3:55 AM
  • User-454825017 posted

    it did it at server side and issue fixed. thanks

    Monday, July 6, 2020 12:18 PM
  • User-454825017 posted

    if i change the file name in url then how js can download the file ?

    as you said 

    var res = str.replace('test.csv','inst.csv');
    Tuesday, July 7, 2020 12:55 PM
  • User288213138 posted

    Hi TDP,

    if i change the file name in url then how js can download the file ?

    Do you mean you only need to set the file name in client pc?

    If so, you can try this code.

    function download(filename, urldata) {
                var a = document.createElement("a");
                a.href = urldata;
                a.setAttribute("download", filename);
                a.click();
            }
    
    download("helloWorld.txt", "data:text/html,HelloWorld!");

    Best regards,

    Sam

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, July 8, 2020 2:49 AM