无法从 angular 8 web API 控制器中的两个不同按钮同时下载 html 文件和 PDF 文件
Unable to download html file and PDF file simultaneously from two different button click in angular 8 web API controller
我有 2 个按钮,一个用于下载 PDF 文件,另一个用于下载 HTML 文件。
当我们点击 link 下载 html 时 html 文件被正确下载,但之后当我们点击 PDF 文件时 link 然后正在下载损坏的 PDF。反之亦然。 (从首先单击的 link 中,link 的文件将正确下载。)
Web API 控制器
[HttpGet]
[Route("ExportPDFFile/{docId}")]
public IActionResult ExportPDFFile(string docId)
{
try
{
// Get the file path
string submissionFilePath =
Path.Combine(_configuration.GetSection("AppSettings").GetValue<string>
("SubmissionDataPath"), docId + ".pdf");
// Send file to client
Stream stream = System.IO.File.OpenRead(submissionFilePath);
if (stream == null)
{
return NotFound(); // returns a NotFoundResult with Status404NotFound response.
}
return new FileStreamResult(stream, "application/octet-stream");
}
catch (Exception ex)
{
SendExceptionEmail(ex);
_logger.LogError("{0}\n{1}", ex.Message, ex.StackTrace);
throw;
}
}
/// <summary>
/// Return the Submission document from the file store
/// </summary>
/// <param name="docId"></param>
/// <returns></returns>
[HttpGet]
[Route("exportHTMLFile/{docId}")]
public IActionResult exportHTMLFile(string docId)
{
try
{
// Get the file path
string submissionHTMLFilePath =
Path.Combine(_configuration.GetSection("AppSettings").GetValue<string>
("SubmissionDataHTMLPath"), docId + ".html");
// Send file to client
Stream stream = System.IO.File.OpenRead(submissionHTMLFilePath);
if (stream == null)
{
return NotFound(); // returns a NotFoundResult with Status404NotFound response.
}
return new FileStreamResult(stream, "application/octet-stream");
}
catch (Exception ex)
{
SendExceptionEmail(ex);
_logger.LogError("{0}\n{1}", ex.Message, ex.StackTrace);
throw;
}
}
app-service.ts
exportPDFFile(id) {
let apiUrl = window.location.href + 'Submission/ExportPDFFile/' + id;
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
//'Authorization': 'jwt-token'
}),
responseType: 'blob' as 'json'
};
return this._http.get<string>(apiUrl, httpOptions);
}
exportHTMLFile(id) {
let apiUrl = window.location.href + 'Submission/exportHTMLFile/' + id;
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
//'Authorization': 'jwt-token'
}),
responseType: 'blob' as 'json'
};
return this._http.get<string>(apiUrl, httpOptions);
}
将 Cordova CLI 安装到版本 >=4.2.0 npm install -g cordova。
这解决了我的问题
我有 2 个按钮,一个用于下载 PDF 文件,另一个用于下载 HTML 文件。
当我们点击 link 下载 html 时 html 文件被正确下载,但之后当我们点击 PDF 文件时 link 然后正在下载损坏的 PDF。反之亦然。 (从首先单击的 link 中,link 的文件将正确下载。)
Web API 控制器
[HttpGet]
[Route("ExportPDFFile/{docId}")]
public IActionResult ExportPDFFile(string docId)
{
try
{
// Get the file path
string submissionFilePath =
Path.Combine(_configuration.GetSection("AppSettings").GetValue<string>
("SubmissionDataPath"), docId + ".pdf");
// Send file to client
Stream stream = System.IO.File.OpenRead(submissionFilePath);
if (stream == null)
{
return NotFound(); // returns a NotFoundResult with Status404NotFound response.
}
return new FileStreamResult(stream, "application/octet-stream");
}
catch (Exception ex)
{
SendExceptionEmail(ex);
_logger.LogError("{0}\n{1}", ex.Message, ex.StackTrace);
throw;
}
}
/// <summary>
/// Return the Submission document from the file store
/// </summary>
/// <param name="docId"></param>
/// <returns></returns>
[HttpGet]
[Route("exportHTMLFile/{docId}")]
public IActionResult exportHTMLFile(string docId)
{
try
{
// Get the file path
string submissionHTMLFilePath =
Path.Combine(_configuration.GetSection("AppSettings").GetValue<string>
("SubmissionDataHTMLPath"), docId + ".html");
// Send file to client
Stream stream = System.IO.File.OpenRead(submissionHTMLFilePath);
if (stream == null)
{
return NotFound(); // returns a NotFoundResult with Status404NotFound response.
}
return new FileStreamResult(stream, "application/octet-stream");
}
catch (Exception ex)
{
SendExceptionEmail(ex);
_logger.LogError("{0}\n{1}", ex.Message, ex.StackTrace);
throw;
}
}
app-service.ts
exportPDFFile(id) {
let apiUrl = window.location.href + 'Submission/ExportPDFFile/' + id;
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
//'Authorization': 'jwt-token'
}),
responseType: 'blob' as 'json'
};
return this._http.get<string>(apiUrl, httpOptions);
}
exportHTMLFile(id) {
let apiUrl = window.location.href + 'Submission/exportHTMLFile/' + id;
let httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
//'Authorization': 'jwt-token'
}),
responseType: 'blob' as 'json'
};
return this._http.get<string>(apiUrl, httpOptions);
}
将 Cordova CLI 安装到版本 >=4.2.0 npm install -g cordova。 这解决了我的问题