![](/img/trans.png)
[英]Can I use a web api from a web application as a service reference?
[英]Pdf from web api service not opening
下面是我的代码,实际上它会下载 pdf 文件,但它可能已损坏或有问题。 你能帮我解决这个问题吗?
public Task<HttpResponseMessage> ExportPDF()
{
HttpResponseMessage response =
Request.CreateResponse(System.Net.HttpStatusCode.BadRequest, "No data to export.");
MemoryStream _stream = new MemoryStream();
try
{
_stream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes("da adasdasd adasd"));
//if file the dowload
if (_stream.Length > 0)
{
response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StreamContent(_stream);
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("inline");
response.Content.Headers.ContentLength = _stream.Length;
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
response.Content.Headers.ContentDisposition.FileName = "Responses.pdf";
}
return Task.FromResult(response);
}
catch (Exception ex)
{
return Task.FromResult(response);
}
}
您在 MemoryStream 中生成一个文本文件。 如果您将“application/pdf”更改为“text/plain” ,它应该可以工作。
如果你真的想在 MemoryStream 中生成一个 PDF 文件,有一些库可以帮助你做到这一点,比如 IronPdf、Aspose、...
谢谢大家,我使用了“SelectPDF”库,现在工作正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.