Skip to content

Instantly share code, notes, and snippets.

@JonathanLoscalzo
Created October 30, 2018 12:27
Show Gist options
  • Select an option

  • Save JonathanLoscalzo/8d3cb47929c6d6e355d58fed9dbcf211 to your computer and use it in GitHub Desktop.

Select an option

Save JonathanLoscalzo/8d3cb47929c6d6e355d58fed9dbcf211 to your computer and use it in GitHub Desktop.
In asp.net mvc, download a file as StreamContent
function print(sells) {
return $http.get("URL", {
params: { ids: sells }, responseType: "arraybuffer"
})
.then(function (res) {
var linkElement = document.createElement('a');
try {
var blob = new Blob([res.data], { type: 'application/octet-stream' });
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", 'mypdf.pdf');
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
} catch (ex) {
console.log(ex);
}
});
}
[HttpGet]
public HttpResponseMessage Print([FromUri] int[] ids)
{
var pdf = this.service.print(ids);
MemoryStream ms = new MemoryStream(pdf);
HttpResponseMessage httpResponseMessage = new HttpResponseMessage();
httpResponseMessage.Content = new StreamContent(ms);
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment