如何从WebAPI传递pdf并从MVC控制器读取pdf? [英] How to pass a pdf from WebAPI and read the pdf from the MVC Controller?

查看:266
本文介绍了如何从WebAPI传递pdf并从MVC控制器读取pdf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应该返回PDF的Web API服务.
然后我试图调用该WebAPI方法以读取PDF.

I have a Web API service that should return a PDF.
I am then trying to call that WebAPI method to read the PDF.

这是我的API方法:

    [HttpPost]
    [Route("GetTestPDF")]
    public HttpResponseMessage TestPDF()
    {
        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
        response.Content = new StreamContent(new FileStream(@"C:\MyPath\MyFile.pdf", FileMode.Open, FileAccess.Read));
        response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
        response.Content.Headers.ContentDisposition.FileName = "MyFile.pdf";
        response.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");

        return Request.CreateResponse(HttpStatusCode.OK, response);
    }

但是,当我阅读回复时,看不到pdf内容.我不确定这是哪里出了错.

However when I go to read the response I don't see the pdf contents. I am not sure where I am going wrong with this.

控制器方法:

public ActionResult GetPDF()
        {
            var response = new HttpResponseMessage();

            using (HttpClient httpClient = new HttpClient())
            {
                httpClient.BaseAddress = new Uri(@"my local host");                               
                response = httpClient.PostAsync(@"api/job/GetTestPDF", new StringContent(string.Empty)).Result;
            }

            var whatisThis = response.Content.ReadAsStringAsync().Result;
            return new FileContentResult(Convert.FromBase64String(response.Content.ReadAsStringAsync().Result), "application/pdf");
        }

当我检查whatis此变量时,我看到了从我的API正确设置的内容类型和内容假象.但是我看不到PDF的内容.

When I examine whatisThis variable I see the content type and the content dispostion correctly set from my API. However I don't see the content of the PDF.

如何阅读PDF内容?

如果我在MVC网站中以字符串形式读取内容,则会看到. (我看不到文件的实际内容)

If I read the content as a string in my MVC site I see. (I don't see the actual content of the file)

{"Version":{"_Major":1,"_Minor":1,"_Build":-1,"_Revision":-1},"Content":{"Headers":[{"Key":"Content-Disposition","Value":["attachment; filename=MyFile.pdf"]},{"Key":"Content-Type","Value":["application/pdf"]}]},"StatusCode":200,"ReasonPhrase":"OK","Headers":[],"RequestMessage":null,"IsSuccessStatusCode":true}

我逐步浏览了WebAPI,它成功读取并设置了响应.带有文件内容的内容.

I stepped through the WebAPI and it is successfully reading and setting the response.Content with the file contents.

仍然不确定这是WebAPI方面还是MVC方面的问题.

Still not sure if this is an issue on the WebAPI side or the MVC side.

推荐答案

我最初将其发布为答案,因为它更容易格式化代码!
我做了一个API端点来返回PDF文件,如果我从浏览器中调用它,该文件将按预期方式打开.
由于您的API似乎没有执行此操作,因此我们假设问题就存在了,因此也就此了.

I'll post this initially as an answer because it's easier to format code!
I made an API endpoint to return a PDF file, and if I call it from a browser the file opens as expected.
As your API doesn't appear to do this, let's assume that the problem is there, and hence this.

这是端点代码,与您的端点代码非常相似,但是缺少ContentDisposition内容:

Here is the endpoint code, that is very similar to yours, but missing the ContentDisposition stuff:

public HttpResponseMessage Get()
{
    HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
    FileStream fileStream = File.OpenRead("FileName.pdf");
    response.Content = new StreamContent(fileStream);
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return response;
}

这篇关于如何从WebAPI传递pdf并从MVC控制器读取pdf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆