Created
October 30, 2018 12:27
-
-
Save JonathanLoscalzo/8d3cb47929c6d6e355d58fed9dbcf211 to your computer and use it in GitHub Desktop.
In asp.net mvc, download a file as StreamContent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | |
| } | |
| }); | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| [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