locked
Download xlsx file from server using Javascript. RRS feed

  • Question

  • User-179964413 posted

    Hello ,

    I work in a SPA . View is in JS and view model is in c#.

    The requirement is that the user will upload an excel file and the application need to validate it and then upload its content in to the Datatabase . I am new to MVVM model so I was not completely aware about it .

    I completed this functionality in the following way

    1. User selectes the respective excel file.

    2. I will send the filpath of the excel file from the Javascript to the Controller ( c#) . There I will try to access that file, validate it using c#.

    3. The rows which fails the validation are highlighted in Yellow and the description of the error is added as a comment in the cell . All this was possible in c#.

    This worked fine when I ran the source code from my local machine . But When I deploy it in the site . It didnt work as I understood that the applicaiton is actually searching for the file path in ther server where the Website is deployed instead of the client system.

    As a quickfix , I thought of copying the excel file from the client system to the server , process the file and copy it back to the client system .

    Is this possible ?

    This is an urgetn fix :( .Please suggest me by providing your ideas . Appreciate your help .

    Thanks,

    Bk

    Wednesday, July 6, 2016 7:57 AM

Answers

  • User-821857111 posted

    I use jQuery to manage this. The following line of code should be added to whatever event handler you want to cause the download:

    $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();

    So if you wanted the download to happen on page load:

    $(function(){
        $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();
    });

    Or on a button click:

    $('button').on('click', function(){
        $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 11, 2016 8:15 AM

All replies

  • User-821857111 posted

    You can't "copy it back to the client system". All you can do is to download it via Javascript*, which will result in the file being saved automatically to the default Downloads location specified by the user's browser.

    *You would use Javascript/jQuery to add an iframe to the DOM of your web age with a style of display:none, and the src attribute will point to the URL of the uploaded Excel file.

    Monday, July 11, 2016 7:18 AM
  • User-179964413 posted

    Hi Mike ,

    Thanks for the response. would be helpful if I can get an example where we can download xlsx file from the server using javascript/jquery.

    Thanks,

    Balakumar

    Monday, July 11, 2016 7:22 AM
  • User-821857111 posted

    I use jQuery to manage this. The following line of code should be added to whatever event handler you want to cause the download:

    $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();

    So if you wanted the download to happen on page load:

    $(function(){
        $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();
    });

    Or on a button click:

    $('button').on('click', function(){
        $('<iframe src="/URL_to_Excel_File"></iframe>').appendTo('body').hide();
    });

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 11, 2016 8:15 AM
  • User-179964413 posted

    Mike ,

    I need to copy the Excel contents to a sql table after some validaiton and processing.  Which of these options work ?

    1. Copying the excel file from client to server and then process the data.

    2. Extract the excel data from the UI side and send it to the server for processing .

    Appreciate your inputs and suggestions.

    Thanks

    Bk

    Tuesday, July 12, 2016 1:57 PM
  • User-821857111 posted

    It's a two stage process. The user has to upload the file for validation, and then they have to make alterations if required and upload it for import. In this type of scenario, you need to make the process as user friendly as possible. They know where the original file is, so you might confuse them if you download a copy with comments in it. They might make changes to that and then attempt to upload the original again. If this was my task, I'd display the validation errors in the browser and let them use that as reference while amending the original.

    Tuesday, July 12, 2016 3:11 PM