返回pdf url作为web api 2查询的结果
在WebAPI项目中,我有一些功能,比如传递URL以供所有人查看pdf返回pdf url作为web api 2查询的结果,pdf,url,asp.net-web-api,asp.net-web-api2,Pdf,Url,Asp.net Web Api,Asp.net Web Api2,在WebAPI项目中,我有一些功能,比如传递URL以供所有人查看pdf [HttpGet] [Route("upload/document")] public HttpResponseMessage GetPdf() { HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK); string f
[HttpGet]
[Route("upload/document")]
public HttpResponseMessage GetPdf()
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
string fileName = System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString() + @"Documents\test.pdf");
FileStream fileStream = File.OpenRead(fileName);
response.Content = new StreamContent(fileStream);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return response;
}
我想要像
但不应该得到网址,当我运行在邮递员它得到的下载
八位字节流
。据推测,使用流会导致浏览器只下载文件。我不得不更改类型application/pdf
内容配置
更改为内联
的标题。因为内容处置已被设置为附件
我自己解决这个问题,这是我的代码
[HttpGet]
[Route("api/test/urlVideo")]
public string urlVideo()
{
string baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) + "/uploads/videos/test.mp4";
return baseUrl;
}
而且工作很完美,所以我
谢谢你的重播,但是你能更详细地解释一下答案吗?在我的代码中我应该在哪里更改。只需添加一个标题
Content-Disposition:inline;filename.pdf
但是有了这段代码,如何获取url?我想要我在问题中提到的url?公共HttpResponseMessage GetPdf(字符串文件){String filename=System.IO.Path.Combine(System.AppDomain.CurrentDomain.BaseDirectory.ToString()+@“Documents\”+文件);}如果你想动态传递url=“/upload/document/{dynamic}.pdf”在您的api中。因此,当用户单击链接时,它将显示pdf而不是下载。
[HttpGet]
[Route("api/test/urlVideo")]
public string urlVideo()
{
string baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) + "/uploads/videos/test.mp4";
return baseUrl;
}