我正在尝试使用带有.Net核心web api的角度2下载PDF(以及后来的单词,excel和图像)文件.我已经粘贴了一些下载文件的代码示例,但是当我尝试打开下载的文件时,它会给我一个损坏的文件.
这是我的.net核心web api代码
public HttpResponseMessage GetTestPdfFile()
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
response.Content = new ByteArrayContent(myArray); //byte[] myArray is byte[] of file downloaded from Azure blob
//response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = myFileName;
response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
return response;
}
Run Code Online (Sandbox Code Playgroud)
我还尝试了另一个代码示例
public FileResult TestDownload(int id)
{
HttpContext.Response.ContentType = "application/pdf";
FileContentResult result = new FileContentResult(myArray, "application/pdf")
{
FileDownloadName = "test.pdf"
};
return result;
}
Run Code Online (Sandbox Code Playgroud)
这就是我在角度服务中所拥有的
getTestPdfFile(): Observable<any> {
let url = 'API_URL'
let headers1 = new …Run Code Online (Sandbox Code Playgroud) 我有一个 Angular 项目,目前正在通过 chrome 调试器在 vs code 内进行调试。我的launch.json用途"preLaunchTask": "serve"是,服务定义为tasks.json,"ng serve"以便我首先执行“ng服务”,并且在调试器打开浏览器以点击http://localhost:4200之前,服务器已准备就绪。
一切正常。但是,当我停止调试时,"ng serve"任务仍然按预期运行,并且每次都必须手动终止该任务。
我尝试使用"postDebugTask"inlaunch.json但不太幸运,因为 vs code 会抱怨启动新任务(postDebug),直到另一个正在运行的任务(preLaunch)未终止。有人可以帮忙吗?