Downloading a file in a C# Ajax application does not work when deployed to server. Running locally is fine and has no issues

0

Making the question more specific with less unknowns: On success in the ajax call, I do a redirect as follows. My issue is that the Controller function is never hit (I put in logging and breakpoints).

NOTE: Please note that this works fine when I run the app locally but not if I deploy to the application server. I get an error that is listed in the original post below.

AJAX Code:

$(document).ready(function () {
        $('#777').on('click', function () {
            $.ajax({
                type: "POST",   
                url: "Report/createFile",
                dataType:"json",
                success: function (data) {
                    var response = data;
                    alert("OK");
                    alert(response.FileGuid);
                    alert(response.FileName);
                    if (response != null &&
                        response.FileGuid != null &&
                        response.FileName != null) {
                        alert("Ready to redirect...");
                        window.location = '/Report/Download?fileGuid=' +  
                             response.FileGuid + '&filename=' + 
                                      response.FileName;

                    }
                    else {
                        alert("Error downloading file");
                    }
                },
                error: function (data) {
                    alert("Report Generation Failed.");
                },
            });
        });
    });

C# controller:

[HttpPost]
public ActionResult createFile()
{
    // Some processing...
    // returning the JSON object 
    return new JsonResult()
    {
        Data = new { FileGuid = handle, FileName = 
                             "TestReportOutput.xlsx" }
    };
}

    [HttpGet]
    public ActionResult Download(string fileGuid, string fileName)
    {
        // !! This code is never reached even though Ajax success 
        // !! function is executed after return from createFile() function
    }

Old post:
I am downloading a file through a C# and ajax web application.  
I used the following for reference:
https://stackoverflow.com/questions/16670209/download-excel-file-via-ajax-mvc

CSL's provided approach worked very well for me.  However, once I published the app to a test server, the download fails with resource not found (404 error).  It is looking for the created file in the physical path c:\inetpub\wwwwroot\Report\Download\TestReportOutput.xlsx and cannot find it.  

i never had to create this physical path when running locally so not sure what I missed.  

Running the app locally is completely fine with no issues.  

This is the error message I get

Detailed Error Information


+ Module    IIS Web Core
+ Notification  MapRequestHandler
+ Handler   StaticFile
+ Error Code    0x80070002
+ Requested URL http://localhost:80/Report/Download?fileGuid=04d40144-db9b-4b96-88c0-0ccb1a080c8c&filename=TestReportOutput.xlsx
+ Physical Path C:\inetpub\wwwroot\Report\Download
+ Logon Method  Negotiate

I am not familiar with intricacies of server deployment so would appreciate any assistance or tips. 
c#
ajax
asked on Stack Overflow Dec 24, 2018 by Bunty • edited Jan 2, 2019 by Bunty

1 Answer

0

static file handler takes your request and watches site directory for file, because of existence of similar directory , so your action will never call. change your download action name and test it.

answered on Stack Overflow Jan 2, 2019 by hvojdani

User contributions licensed under CC BY-SA 3.0