使用MVC httpresponse和angularjs下载pdf [英] Download pdf using MVC httpresponse and angularjs
问题描述
我尝试从以下方式..plz让我知道我错在哪里.....
c#
。 ...
I tried from the following way..plz get me idea where i am wrong.....
c#
....
byte[] bytes = memoryStream.ToArray();
memoryStream.Close();
httpResponseMessage.Content = new ByteArrayContent(bytes.ToArray());
httpResponseMessage.Content.Headers.Add("x-filename", fileName);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/octet-stream");
httpResponseMessage.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
httpResponseMessage.Content.Headers.ContentDisposition.FileName = fileName;
httpResponseMessage.StatusCode = HttpStatusCode.OK;
return httpResponseMessage;
in angularjs
in angularjs
$http({
method: 'post',
url: basePath + '/profile/downloadpdf_fromedit',
// data: JSON.stringify($scope.paginginfostmntaccnt),
responsetype: 'arraybuffer',
headers: {'content-type': 'application/pdf'},
// headers: { 'content-type': 'application/json' }
})
.then(function (response) {
// console.log(response.data);
$scope.dataDetail = response.data;
console.log($scope.dataDetail)
1. var file = new Blob([response.data], { type: 'application/pdf' });
saveAs(file, 'StatementofAccount.pdf');
//url-file:///C:/Users/tushar/Downloads/StatementofAccount.pdf
//无法加载pdf
//url-file:///C:/Users/tushar/Downloads/StatementofAccount.pdf
//failed to load pdf
2.var file = new Blob([response], { type: 'application/pdf' });
var fileurl = URL.createObjectURL(file);
window.open(fileurl);
// url-blob:http:// localhost:16311 / 02f8d85e-74c0-4ccd-b937-22f02cc3866c
//无法加载pdf文件
3.
//url- blob:http://localhost:16311/02f8d85e-74c0-4ccd-b937-22f02cc3866c
//failed to load pdf document
3.
.success(function (data, status, headers, config) {
// any required additional processing here
var results = [];
results.data = data;
results.headers = headers();
results.status = status;
results.config = config;
console.log(results)
$("#loading").hide();
headers = headers();
var filename = headers['x-filename'];
var contentType = headers['content-type'];
if (!filename) {
filename = headers["x-filename"] || 'statementAccount.pdf';
}
var linkElement = document.createElement('a');
try {
var blob = new Blob([data], { type: contentType });
var url = window.URL.createObjectURL(blob);
linkElement.setAttribute('href', url);
linkElement.setAttribute("download", filename);
var clickEvent = new MouseEvent("click", {
"view": window,
"bubbles": true,
"cancelable": false
});
linkElement.dispatchEvent(clickEvent);
$("#loading").hide();
console.log("filename" + filename);
} catch (ex) {
console.log("catch"+ex);
$("#loading").hide();
}
})
//url-file:///C:/Users/tushar/Downloads/statementAccount.pdf
//无法加载pdf文件
我尝试过:
我试过的代码我已经包含在查询中。
//url-file:///C:/Users/tushar/Downloads/statementAccount.pdf
//failed to load pdf document
What I have tried:
The code I have tried i have included in query.
推荐答案
http({
method:' post',
url:basePath + ' / profile / downloadpdf_fromedit',
// data: JSON.stringify(
http({ method: 'post', url: basePath + '/profile/downloadpdf_fromedit', // data: JSON.stringify(
scope.paginginfostmntaccnt),
responsetype:' arraybuffer',
header:{' content-type':'application / pdf'},
// header :{'content-type':'application / json'}
})
.then( function (response){
// console.log(response.data);
scope.paginginfostmntaccnt), responsetype: 'arraybuffer', headers: {'content-type': 'application/pdf'}, // headers: { 'content-type': 'application/json' } }) .then(function (response) { // console.log(response.data);
scope.dataDetail = response.data;
console .log(
scope.dataDetail = response.data; console.log(
这篇关于使用MVC httpresponse和angularjs下载pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!