0

I have below code to download file from S3

 public FileStreamResult Index(string key, string fileName)
        {          

            GetObjectResponse resp = ReadS3Object("data", key);
            StreamReader sr = new StreamReader(resp.ResponseStream);

            var filresult = File(resp.ResponseStream, resp.ContentType);
            Response.Clear();
            Response.Buffer = true;
            Response.ContentType = "application/vnd.ms-excel";
            Response.AddHeader("content-disposition", "attachment; filename=fileName);
            Response.Flush();
            return filresult;

        }

and Ajax call like below

 $.ajax(
         {
             url: '/DownloadEstimation/Index',
             contentType: 'application/json; charset=utf-8',
             datatype: 'json',
             data: {
                 key: key,
                 fileName: fname
             },
             type: "GET",
             success: function () {
                   window.location = '/DownloadEstimation/'+ key + '/' + fname;
             }
         });

But it just call method, not prompting user to save the file

Any help highly appreciated.

Thanks

Md. Parvez Alam
  • 4,326
  • 5
  • 48
  • 108
  • How is it duplicate. the scenario is different, I am trying code base but not working – Md. Parvez Alam Aug 01 '17 at 11:10
  • The answers clearly state that you **cannot** use ajax to download a file using ajax. Use `location.href='/DownloadEstimation/Index?key=xx&fileName=yy` –  Aug 01 '17 at 11:16
  • that I tried,but its not downloading file – Md. Parvez Alam Aug 01 '17 at 11:18
  • Your edit shows your url would not call the `Index` method (and do have a route definition for that method?). And there is no point calling the method twice. Just use the code in my previous comment (not in an ajax call) –  Aug 01 '17 at 11:23

0 Answers0