0

I have post the similar question yesterday and i haven't get the results. I have loaded data on my kendo grid and when i click download, i want to download the file but it is not returning results. The folder that i am downloading from is on the server not on project solution. I created a controller to test the download without a button click and it works but i want to download from kendo button click. No error on console

Function for getting the selected Id from the grid

 function showDetails(e) {
               e.preventDefault();
               var dataItem = this.dataItem($(e.currentTarget).closest("tr"));

               DownloadIndexController(dataItem.possID);
               }

Ajax call to the controller

function DownloadIndexController(possID) {
                $.ajax({
                    url: '@Url.Action("DownloadIndex", "Poss")',
                    type: "GET",
                    data: { possID: possID },
                    dataType: "json",
                    success: function (data) {
                        window.location = '@Url.Action("DownloadIndex", "Poss")';
                    }
                })
            }

Controller

public ActionResult DownloadIndex(int possID)
    {
        string Filelocation = "myserverlocation"
        FileContentResult ff = null;
        try
        {
            OnePossModel md = new Models.OnePossModel();
            JsonParamBuilder myBuilder = new JsonParamBuilder();
            myBuilder.AddParam<Guid>("submittingUserID", System.Guid.Parse(User.Identity.GetUserId()));
            myBuilder.AddParam<int>("possID", Convert.ToInt32(possID));

            string jsonReq = Models.JsonWrapper.JsonPOST(ApiBaseUrl + ApiPOSSSubBaseUrl + "/WritePOSSFile", myBuilder.GetJSonParam());
            string possFilename = Models.DeserialiseFromJson<string>.DeserialiseApiResponse(jsonReq);

            possFilename = possFilename.Replace(",", ",");

            string x = Filelocation + possFilename.ToString();

            var type = System.Net.Mime.MediaTypeNames.Application.Octet;

            byte[] fileBytes = System.IO.File.ReadAllBytes(x);
            string myfileName = possFilename;
            ff =  File(fileBytes, type,myfileName);
            Response.AppendHeader("Content-Disposition", "attachment; filename=" + myfileName);
            Response.BinaryWrite(fileBytes);
            return ff;
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }               
Mr Junior
  • 81
  • 2
  • 17

2 Answers2

0

In your controller, just add this:

public FileResult DownloadIndex(int possID)
{
    . . . .
    return File(fileBytes, type,myfileName);
}
SᴇM
  • 7,024
  • 3
  • 24
  • 41
0

I don't think it can be done the way you are trying to. Take a look here for a workaround to simulate "ajax file download".

In your code you are making 2 requests, the first creates the file and stream it in the response (and cant be downloaded with ajax), and then the second request by setting the window.location - But the file is not "alive" any more since it was allocated to the first response.

If using the FileResult Action then give away the ajax call (depends on your requirements) and just use a plain link: /poss/downloadindex/123

Community
  • 1
  • 1
Issac
  • 943
  • 1
  • 6
  • 13
  • @Isasac I don't really understand what you saying. can you just modify the code so that i can understand on what you are saying. thanks – Mr Junior Dec 20 '16 at 19:45