Trying to create a function that download a file existing in a server using web api. The api seems to be returning the data but when I try to use javascript to download the file and open it with Ms Word I always get an error. 

Below is the method to get the file using web api

```
 private HttpResponseMessage ReturnFile(string filename)
    {
        //FileStream stream = new FileStream(HttpContext.Current.Server.MapPath("~") + "\\Temp" + "\\" + filename, FileMode.Open, FileAccess.Read, FileShare.None);
        HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);

        //Set the File Path.
        string filePath = HttpContext.Current.Server.MapPath("~") + "\\Temp" + "\\" + filename;

        //Check whether File exists.
        if (!File.Exists(filePath))
        {
            //Throw 404 (Not Found) exception if File not found.
            response.StatusCode = HttpStatusCode.NotFound;
            response.ReasonPhrase = string.Format("File not found: {0} .", filename);
            throw new HttpResponseException(response);
        }

        //Read the File into a Byte Array.
        byte[] bytes = File.ReadAllBytes(filePath);

        //Set the Response Content.
        response.Content = new ByteArrayContent(bytes);

        //Set the Response Content Length.
        response.Content.Headers.ContentLength = bytes.LongLength;

        //Set the Content Disposition Header Value and FileName.
        response.Content.Headers.Add("x-filename", filename); //We will use this below
        response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = filename;
        response.Content.Headers.Add("Access-Control-Expose-Headers", "x-filename");
        //Set the File Content Type.
        response.Content.Headers.ContentType = new MediaTypeHeaderValue(MimeMapping.GetMimeMapping(filename));
        return response;
    }

```

Below is the post function that calls the web api

```
 function CallPostRestAPIDownload(url, data, callbacksuccess, callbackerror) {
    $.ajax({
        type: "POST",
        async: false,
        url: url,
        contentType: "application/json",
        dataType: "text",
        data: JSON.stringify(data),
        error: callbackerror,
        success: callbacksuccess
    });
}

```

Below is the function to force download the file

```
function DownloadDoc(data, status, xhr) {
    console.log("data:" + JSON.stringify(data));
    console.log("status:" + JSON.stringify(status));
    console.log("xhr:" + JSON.stringify(xhr));
    var blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' });

    var downloadUrl = URL.createObjectURL(blob);
    let filename = "";// headers['x-filename'];
    console.log("getAllResponseHeaders:" + xhr.getAllResponseHeaders());
    //console.log("getResponseHeader:" + xhr.getResponseHeader("x-filename"));
    filename = xhr.getResponseHeader('x-filename');

    console.log(filename);
    let hiddenElement = document.createElement('a');
    hiddenElement.href = downloadUrl;
    hiddenElement.target = '_blank';

    hiddenElement.download = filename;
    hiddenElement.click();
    //window.open(uri, 'newdownload.csv');
}

```

When I open the file I got this error

[![enter image description here](https://i.stack.imgur.com/4HSFt.png)](https://i.stack.imgur.com/4HSFt.png)

This is what is the force download returns
[![enter image description here](https://i.stack.imgur.com/hOIAD.png)](https://i.stack.imgur.com/hOIAD.png)