C# 如何从Asp.net MVC中的API获取PDF文件?
我想从ASP.NETCore中的API获取一个pdf文件,但我不知道怎么做。 我所做的是向api发送一个模型,然后收到一个pdf文件 这是我的API中返回文件的方法(在Postman中是有效的) 这是我在web应用程序中的代码,但它不起作用C# 如何从Asp.net MVC中的API获取PDF文件?,c#,asp.net-mvc,model-view-controller,asp.net-mvc-5.2,C#,Asp.net Mvc,Model View Controller,Asp.net Mvc 5.2,我想从ASP.NETCore中的API获取一个pdf文件,但我不知道怎么做。 我所做的是向api发送一个模型,然后收到一个pdf文件 这是我的API中返回文件的方法(在Postman中是有效的) 这是我在web应用程序中的代码,但它不起作用 [HttpGet] public FileResult getPDF(int factura) { this.GetSession(); // Object sent to the w
[HttpGet]
public FileResult getPDF(int factura)
{
this.GetSession();
// Object sent to the web api
FacturaRequest oFacturaRequest = new FacturaRequest();
oFacturaRequest.Token = oUserSession.accessToken;
oFacturaRequest.IdFactura = factura;
RequestUtil oRequestUtil = new RequestUtil();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = JsonConvert.SerializeObject(oFacturaRequest); // Convert model to json format
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.Url.GETFACTURA);
request.Method = "POST";
request.PreAuthenticate = false;
request.Accept = "application/json";
request.MediaType = "application/json";
request.ContentType = "application/json; charset=utf-8";
request.Timeout = 60000;
using (var oStreamWriter = new StreamWriter(request.GetRequestStream()))
{
oStreamWriter.Write(json);
oStreamWriter.Flush();
}
HttpWebResponse oHttpResponse = (HttpWebResponse)request.GetResponse();
var result = "";
using (var oStreamReader = new StreamReader(oHttpResponse.GetResponseStream()))
{
result = oStreamReader.ReadToEnd();
}
return File(result,"application/pdf","Factura");
}
是否要创建一个pdf文件,其内容基于从“Factura”web请求返回给您的JSON内容?如果我理解正确,您将需要使用收到的JSON内容从头开始构建pdf文件。一个免费的库,可以帮助您使用此itextpdf7。这里有一个链接到官方网站上的一个示例-不,我想要的是接收来自API的pdf。您想创建一个pdf文件,其内容基于从“Factura”web请求返回给您的JSON内容吗?如果我理解正确,您将需要使用收到的JSON内容从头开始构建pdf文件。一个免费的库,可以帮助您使用此itextpdf7。这里有一个链接到官方网站上的一个示例-不,我想要的是接收来自api的pdf
[HttpGet]
public FileResult getPDF(int factura)
{
this.GetSession();
// Object sent to the web api
FacturaRequest oFacturaRequest = new FacturaRequest();
oFacturaRequest.Token = oUserSession.accessToken;
oFacturaRequest.IdFactura = factura;
RequestUtil oRequestUtil = new RequestUtil();
JavaScriptSerializer js = new JavaScriptSerializer();
string json = JsonConvert.SerializeObject(oFacturaRequest); // Convert model to json format
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Constants.Url.GETFACTURA);
request.Method = "POST";
request.PreAuthenticate = false;
request.Accept = "application/json";
request.MediaType = "application/json";
request.ContentType = "application/json; charset=utf-8";
request.Timeout = 60000;
using (var oStreamWriter = new StreamWriter(request.GetRequestStream()))
{
oStreamWriter.Write(json);
oStreamWriter.Flush();
}
HttpWebResponse oHttpResponse = (HttpWebResponse)request.GetResponse();
var result = "";
using (var oStreamReader = new StreamReader(oHttpResponse.GetResponseStream()))
{
result = oStreamReader.ReadToEnd();
}
return File(result,"application/pdf","Factura");
}