1

I have following code in my C# web api

HttpResponseMessage result = null;
var stream = new MemoryStream(File.ReadAllBytes(tempFileName));
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
result.Content.Headers.add("fileName", Path.GetFileName(tempFileName ));
File.Delete(tempFileName);
return result;

and following on client side

var deferred = this.$q.defer();
this.$http({
              method: 'GET',
              url: someURL,
              responseType: 'arraybuffer'
}).then(function (response: any) {
     var blob = new Blob([response.data], { type: "application/octet-stream" });
     var link = document.createElement('a');
     var url = URL.createObjectURL(blob);
     link.setAttribute('href', url);
     link.setAttribute("download", "demo.doc");
     link.click();
     deferred.resolve();
   });
   return deferred.promise;

But when I add watch for response.headers() on client side javascript it only shows me following

{content-type: "application/octet-stream", cache-control: "private"}

Can some one help me find where the fileName header is?

nikhil mehta
  • 972
  • 15
  • 30

1 Answers1

0

First thing I noticed is your the 'add' should 'Add' which I guess may be a typo at your end but wanted to point out.

result.Content.Headers.Add("fileName", Path.GetFileName(tempFileName ));

As I can see that you are using angular's $http service to make your server ajax calls, you can use the 'headers' property of the response object as following.

response.headers('fileName')

If you are using jQuery, then you can simply use following where jqXHR is the jQuery's jqXHR object, which is a superset of the XMLHTTPRequest object.

jqXHR.getResponseHeader('filename')
Vivek N
  • 991
  • 12
  • 22
  • Thanks for the answer but neither of the solution is working as you can see only two header are exposed to client side. I am not sure where to make changes server side or client side – nikhil mehta Jun 06 '16 at 08:22
  • I would try to intercept the network traffic (you can use a Chrome's Developer Tools or Fiddler etc to do it) and then check out the http respone headers. If you don't see your custom header 'filename' there, then most likely it is not being passed from the server. – Vivek N Jun 06 '16 at 12:46