我有一个生成PDF的动作类.该contentType适当地设定.
public class MyAction extends ActionSupport
{
public String execute() {
...
...
File report = signedPdfExporter.generateReport(xyzData, props);
inputStream = new FileInputStream(report);
contentDisposition = "attachment=\"" + report.getName() + "\"";
contentType = "application/pdf";
return SUCCESS;
}
}
Run Code Online (Sandbox Code Playgroud)
我action 通过Ajax调用来调用它.我不知道将此流传递给浏览器的方法.我尝试过一些东西,但没有任何效果.
$.ajax({
type: "POST",
url: url,
data: wireIdList,
cache: false,
success: function(response)
{
alert('got response');
window.open(response);
},
error: function (XMLHttpRequest, textStatus, errorThrown)
{
alert('Error occurred while opening fax template'
+ getAjaxErrorString(textStatus, errorThrown));
}
});
Run Code Online (Sandbox Code Playgroud)
以上给出了错误:
您的浏览器发送了此服务器无法理解的请求.
我从WebAPI控制器返回一个文件.Content-Disposition标头值自动设置为"attachment".例如:
性格:依恋; 文件名= "30956.pdf"; 文件名*= UTF-8''30956.pdf
当它设置为附件时,浏览器将要求保存文件而不是打开它.我想打开它.
如何将其设置为"内联"而不是"附件"?
我正在使用此方法发送文件:
public IActionResult GetDocument(int id)
{
var filename = $"folder/{id}.pdf";
var fileContentResult = new FileContentResult(File.ReadAllBytes(filename), "application/pdf")
{
FileDownloadName = $"{id}.pdf"
};
// I need to delete file after me
System.IO.File.Delete(filename);
return fileContentResult;
}
Run Code Online (Sandbox Code Playgroud) 我在ASP.Net核心中创建了Wep API以返回PDF.这是我的代码:
public HttpResponseMessage Get(int id)
{
var response = new HttpResponseMessage(System.Net.HttpStatusCode.OK);
var stream = new System.IO.FileStream(@"C:\Users\shoba_eswar\Documents\REquest.pdf", System.IO.FileMode.Open);
response.Content = new StreamContent(stream);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = "NewTab";
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
return response;
}
Run Code Online (Sandbox Code Playgroud)
但它只返回JSON响应:
{
"version":{
"major":1,
"minor":1,
"build":-1,
"revision":-1,
"majorRevision":-1,
"minorRevision":-1
},
"content":{
"headers":[
{
"key":"Content-Disposition",
"value":[
"attachment; filename=NewTab"
]
},
{
"key":"Content-Type",
"value":[
"application/pdf"
]
}
]
},
"statusCode":200,
"reasonPhrase":"OK",
"headers":[
],
"requestMessage":null,
"isSuccessStatusCode":true
}
Run Code Online (Sandbox Code Playgroud)
我在这里做错了吗?