Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
返回pdf url作为web api 2查询的结果_Pdf_Url_Asp.net Web Api_Asp.net Web Api2 - Fatal编程技术网

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

返回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

在WebAPI项目中,我有一些功能,比如传递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;

        }
我想要像 但不应该得到网址,当我运行在邮递员它得到的下载

  • mime类型不需要是
    八位字节流
    。据推测,使用流会导致浏览器只下载文件。我不得不更改类型
    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;
    }