我正在尝试从服务器下载 pdf 文件并在浏览器中显示它。我的 web api 方法将文件作为 httpResponseMessage 返回,并且工作正常,因为它返回文件。但在 AngularJs 端我无法显示该文件。有人可以帮助我理解我错过了什么吗?
Web Api 方法:
public HttpResponseMessage GetHelpReferenceDocs(Guid streamKey)
{
var fakeFileName = GetStream(streamKey); // If this succeeds, stream is known and unexpired.
// Internal file name
string staticFileName = helpFiles[fakeFileName];
var mappedPath = System.Web.Hosting.HostingEnvironment.MapPath("~/Static/" + staticFileName);
HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
var stream = new FileStream(mappedPath, FileMode.Open, FileAccess.Read, FileShare.Read);
result.Content = new StreamContent(stream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentDisposition.FileName = Path.GetFileName(mappedPath);
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
result.Content.Headers.ContentLength = stream.Length;
return result;
}
AngularJS:
function loadDocument(fileName) {
REST.post(commonService.constants.webapi.helpFileStreamKey + fileName)
.then(function(response) {
var streamGuid = response.data;
REST.get(commonService.constants.webapi.helpReferenceGuide + streamGuid).then(function (response) {
$window.open(response.data);
});
})
.catch(function (e) { $scope.errorHandler(moduleName, e); })
.finally($scope.waitOn);
}
最佳答案
看看建议 here ,或者您可以尝试专门用于显示 pdf 的 npm 模块,该模块已经为您处理好了,such as this one.
关于javascript - 使用 angularjs 显示 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42139795/