我正在尝试在新的弹出窗口中显示图像和 pdf 文件。该文档以字节数组的形式保存在数据库中。这是我的服务器端代码。
public HttpResponseMessage GetUserDocumentByDocumentId(int documentId)
{
HttpResponseMessage result = null;
try
{
RegistrationManager registrationManager = new RegistrationManager();
var file = registrationManager.GetUserDocumentByDocumentId(documentId);
result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = result.Content = new ByteArrayContent(file.DocumentObject);
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
result.Content.Headers.ContentType = new MediaTypeHeaderValue(file.DocumentExtension);
result.Content.Headers.ContentDisposition.FileName = file.DocumentName;
result.Content.Headers.Add("x-filename", file.DocumentName);
return result;
}
catch (Exception ex)
{
throw new HttpResponseException(FBSHttpResponseException.HttpResponseMessage(ex));
}
}
客户端代码是这样的
var windowWidth = 1000;
var windowHeight = 550;
var left = (screen.width / 2) - (windowWidth / 2);
var top = ((screen.height / 2) - (windowHeight / 2)) - 50;
if (typeof reportWindow != 'undefined' && reportWindow.closed == false) {
reportWindow.close();
}
var url = baseUrl + "/api/user/getuserdocumentbydocumentid/" + userDocId;
$window.open(url, 'PopupReport', 'scrollbars=yes,status=yes,toolbar=yes,menubar=no,location=no,resizable=no,fullscreen=yes, width=' + windowWidth + ', height=' + windowHeight + ', top=' + top + ', left=' + left);
但它不是在新窗口中显示它们,而是打开窗口并下载 pdf 或图像文件。
最佳答案
您应该在后端将 Content-Disposition header 设置为“内联”,以便浏览器尝试呈现文件而不是下载它:
result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
查看这个问题Content-Disposition:What are the differences between "inline" and "attachment"?
关于javascript - Angularjs 在新的弹出窗口中显示 pdf/图像文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37048124/