1

I am trying to download a file from server using window.open(path,'_blank','download') but it just opens it in a new tab. How do I download the file? Yes I did check for other similar question but none of them work. Also I've tried this but it didn't work.

$scope.docView = function () {     
    Method.getbyId("api call",docId).then(function(response) {                
        }).catch(function (data) {
            console.log("Unknown Error");
        });
    }
}

/*this.getbyId = function (path, id) {
            return $http.get(appSetting.apiBaseUrl + path + "/" + id);
        };
*/




[Route("api call")]
    [HttpGet]
    public IHttpActionResult ViewDocument (Guid? docId)
    {
          /*background work*/            

            response.Message = filePath;
            var bytes=System.IO.File.ReadAllBytes(prevPath);                
            HttpContext.Current.Response.Buffer = true;
            HttpContext.Current.Response.Clear();
            HttpContext.Current.Response.ClearHeaders();
            HttpContext.Current.Response.ContentType = value.Format;
            string Name = value.DocumentName;
            HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=" + Name);               
            HttpContext.Current.Response.BinaryWrite(bytes);
            }                
        }
        catch (Exception ex)
        {
            Utils.Write(ex);
        }

        return Ok(response);
    }
Shalini Raj
  • 137
  • 2
  • 13
  • Code please.... – Luke Mar 02 '18 at 09:02
  • 1
    In your question yesterday (in the comments) https://stackoverflow.com/questions/49045557/how-to-view-a-file-located-on-server-on-click-of-a-button you asked for the exact opposite of this...""The problem is I should not download the file, it should just popup in a new tab in the browser ". Have you changed your mind? But as I already explained, there is only a limited extent to which this is in your control, some of it is to do with the browser. You can try to affect it with the headers as per the answer below but that's about all you can do. – ADyson Mar 02 '18 at 09:13
  • yes actually now i just have to download the file. it should actually come as a download. – Shalini Raj Mar 02 '18 at 09:19
  • Possible duplicate of [Is it possible to initiate a download prompt in browser for recognized mime-types using only JavaScript (client-side approach)?](https://stackoverflow.com/questions/7763505/is-it-possible-to-initiate-a-download-prompt-in-browser-for-recognized-mime-type) – Liam Mar 02 '18 at 10:01
  • Actually i cannot use Download cz I'm using this button inside syncfusion grid which will go to jsrender . from there a function is called which will click a hidden button to go to angularJS controller. in any case i cant use that. – Shalini Raj Mar 02 '18 at 10:19

1 Answers1

1

To force the browser to download the file (instead of displaying it, in another tab or the current one) requires a special header to be sent along with the file body.

That's only possible if you can modify some things server-side.

You should send following headers :

  • Content-Disposition: attachment; filename"myfile.txt"
  • Content-Type: application/octet-stream; name="myfile.txt"
  • Content-Transfer-Encoding: binary

Of course, replace application/octet-stream by the content-type of your file, if known (application/pdf, image/jpeg, etc.)

Pierre
  • 604
  • 1
  • 7
  • 14
  • can you tell where to write the above headers ? like in my code i have written some headers. will it be fine ? what else to write to download ? – Shalini Raj Mar 02 '18 at 09:51
  • this may or may not work. There is no way to "force" a browser to download a file. You have no control over this functionality it's solely down to how the user has setup his or her browser settings. This may help by allowing the browser to make a more informed decision but it's wrong to say that this will "force the browser to download the file" – Liam Mar 02 '18 at 09:59
  • yes, there's no way to "force" (the browser may override the behavior with its own settings), but this will work with a 99% rate. Never encountered an issue with that code. – Pierre Mar 03 '18 at 08:47