Return pdf url 作为网络 api 2 查询的结果

Return pdf url as result of web api 2 query

在网络 API 项目中,我有像 pass URL 这样的功能供所有人查看 pdf,

        [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;

        }

我想要url喜欢http://localhost:54316/upload/document/test.pdf 但不应该得到 url,当我在 Postman 中 运行 时,它会下载。

  1. MIME 类型不能是 octet-stream。据推测,使用流会导致浏览器只下载文件。我不得不更改类型 application/pdf.
  2. 添加一个 header,将 content-disposition 更改为 inline。因为 content-disposition 已经设置为 attachment
string mimeType = "application/pdf"
Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);

我自己解决了这个问题,这是我的代码

[HttpGet]
[Route("api/test/urlVideo")]
public string urlVideo()
{
  string baseUrl = Request.RequestUri.GetLeftPart(UriPartial.Authority) + "/uploads/videos/test.mp4";
            return baseUrl;
}

成功了 perfect.So 我明白了

http://hostname/uploads/documents/text.pdf