代码之家  ›  专栏  ›  技术社区  ›  Brijesh Mavani

返回pdf url作为web api 2查询的结果

  •  1
  • Brijesh Mavani  · 技术社区  · 6 年前

    在web API项目中,我有一些功能,如传递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,当我在邮递员中运行时,它会得到下载。

    2 回复  |  直到 6 年前
        1
  •  1
  •   Suseendran Kandasamy    6 年前
    1. mime类型必须不是 octet-stream 。据说,使用流会导致浏览器只下载文件。我必须改变类型 application/pdf
    2. 添加更改 content-disposition inline 。因为内容处置已设置为 attachment
    string mimeType = "application/pdf"
    Response.AppendHeader("Content-Disposition", "inline; filename=" + fileName);
    
        2
  •  0
  •   Brijesh Mavani    6 年前

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

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

    这是完美的工作。所以我得到

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