从Web API服务下载pdf文件时出现问题

用户名

我正在尝试设置一个Web API服务,该服务在目录中搜索.pdf文件,如果找到该文件,则返回该文件。

控制器

public class ProductsController : ApiController
{

    [HttpPost]
    public HttpResponseMessage Post([FromBody]string certificateId)
    {
        string fileName = certificateId + ".pdf";
        var path = @"C:\Certificates\20487A" + fileName;

        //check the directory for pdf matching the certid
        if (File.Exists(path))
        {
            //if there is a match then return the file
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.OK);
            var stream = new FileStream(path, FileMode.Open);
            stream.Position = 0;
            result.Content = new StreamContent(stream);
            result.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment") { FileName = fileName };
            result.Content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf");
            result.Content.Headers.ContentDisposition.FileName = fileName;
            return result;
        }
        else
        {
            HttpResponseMessage result = new HttpResponseMessage(HttpStatusCode.Gone);
            return result;
        }
    }
}

我使用以下代码调用服务

private void GetCertQueryResponse(string url, string serial)
{
    string encodedParameters = "certificateId=" + serial.Replace(" ", "");

    HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);

    httpRequest.Method = "POST";
    httpRequest.ContentType = "application/x-www-form-urlencoded";
    httpRequest.AllowAutoRedirect = false;

    byte[] bytedata = Encoding.UTF8.GetBytes(encodedParameters);
    httpRequest.ContentLength = bytedata.Length;

    Stream requestStream = httpRequest.GetRequestStream();
    requestStream.Write(bytedata, 0, bytedata.Length);
    requestStream.Close();

    HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();

    if (response.StatusCode == HttpStatusCode.OK)
    {

        byte[] bytes = null;
        using (Stream stream = response.GetResponseStream())
        using (MemoryStream ms = new MemoryStream())
        {
            int count = 0;
            do
            {
                byte[] buf = new byte[1024];
                count = stream.Read(buf, 0, 1024);
                ms.Write(buf, 0, count);
            } while (stream.CanRead && count > 0);
            ms.Position = 0;
            bytes = ms.ToArray();
        }

        var filename = serial + ".pdf";

        Response.ContentType = "application/pdf";
        Response.Headers.Add("Content-Disposition", "attachment; filename=\"" + filename + "\"");
        Response.BinaryWrite(bytes);
    }
}

从正确的文件名和大小等显示下载文件对话框的意义上来说,这似乎是可行的,但是下载仅需几秒钟(当文件大小> 30mb时),并且当我尝试时文件已损坏打开它们。

有什么想法我做错了吗?

智者

您的代码与Ive过去使用的代码相似,但以下是我通常使用的代码:

    Response.AddHeader("content-length", myfile.Length.ToString())
    Response.AddHeader("content-disposition", "inline; filename=MyFilename")
    Response.AddHeader("Expires", "0")
    Response.AddHeader("Pragma", "Cache")
    Response.AddHeader("Cache-Control", "private")

    Response.ContentType = "application/pdf"

    Response.BinaryWrite(finalForm)

我发布此邮件有两个原因。首先,添加content-length标头,您可能必须指出文件的大小,以便应用程序等待整个响应。

如果那不能解决问题。设置一个断点,字节数组是否满足适当的长度(对于30 MB的文件,又为3000万字节)?您是否曾使用提琴手来查看HTTP调用返回了多少内容?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

从Web API服务下载pdf文件时出现问题

来自分类Dev

Moodle REST Web服务:发送数组作为POST参数时出现问题

来自分类Dev

使我的Web服务器从外部设备工作时出现问题

来自分类Dev

在CentOS中设置Web服务器时出现问题

来自分类Dev

从Mac访问在VirtualBox中发布的Web服务时出现问题

来自分类Dev

使用DataContext文件和webConfig文件将Web App与localdb连接时出现问题

来自分类Dev

在Web Api ActionFilters上使用简单注入器注入属性时出现问题

来自分类Dev

在IIS上托管.net5.0核心Web API时出现问题(10.0.17763.1)

来自分类Dev

将对象从JavaScript传递到Web API时出现问题

来自分类Dev

下载多个PDF时出现问题

来自分类Dev

设置tornado.web.Application时出现问题

来自分类Dev

在Spyder IDE中运行Web scraper时出现问题

来自分类Dev

将数据作为Angular JS中的表单数据发布到Java REST Web服务时出现问题

来自分类Dev

OAuth2 Adal Angularjs Web APi功能出现问题-Web APi未授权

来自分类Dev

下载文件时出现401 Web异常

来自分类Dev

下载文件时出现401 Web异常

来自分类Dev

使用Sails js显示和下载PDF文件时出现问题

来自分类Dev

通过AJAX访问Web服务时出现标题问题

来自分类Dev

通过docusign API在我的服务器上下载签名文档的副本时出现问题

来自分类Dev

在Web应用程序服务器(php)中使用maptlotlib出现问题

来自分类Dev

在Angular 8中从web.api下载文件时从服务器获取文件名

来自分类Dev

Web API下载锁定文件

来自分类Dev

使用Python通过Google Drive API下载文件时出现问题

来自分类Dev

使用内容处置下载文件时出现问题

来自分类Dev

下载zip文件ASP MVC时出现问题

来自分类Dev

下载文件时出现问题(java)

来自分类Dev

将Web抓取到CSV时出现问题[AttributeError:“ str”对象没有属性“ text”

来自分类Dev

将mysql数据库与基于Java的Web应用程序链接时出现问题

来自分类Dev

将Matlab与Java集成以在Web中进行部署时出现问题

Related 相关文章

  1. 1

    从Web API服务下载pdf文件时出现问题

  2. 2

    Moodle REST Web服务:发送数组作为POST参数时出现问题

  3. 3

    使我的Web服务器从外部设备工作时出现问题

  4. 4

    在CentOS中设置Web服务器时出现问题

  5. 5

    从Mac访问在VirtualBox中发布的Web服务时出现问题

  6. 6

    使用DataContext文件和webConfig文件将Web App与localdb连接时出现问题

  7. 7

    在Web Api ActionFilters上使用简单注入器注入属性时出现问题

  8. 8

    在IIS上托管.net5.0核心Web API时出现问题(10.0.17763.1)

  9. 9

    将对象从JavaScript传递到Web API时出现问题

  10. 10

    下载多个PDF时出现问题

  11. 11

    设置tornado.web.Application时出现问题

  12. 12

    在Spyder IDE中运行Web scraper时出现问题

  13. 13

    将数据作为Angular JS中的表单数据发布到Java REST Web服务时出现问题

  14. 14

    OAuth2 Adal Angularjs Web APi功能出现问题-Web APi未授权

  15. 15

    下载文件时出现401 Web异常

  16. 16

    下载文件时出现401 Web异常

  17. 17

    使用Sails js显示和下载PDF文件时出现问题

  18. 18

    通过AJAX访问Web服务时出现标题问题

  19. 19

    通过docusign API在我的服务器上下载签名文档的副本时出现问题

  20. 20

    在Web应用程序服务器(php)中使用maptlotlib出现问题

  21. 21

    在Angular 8中从web.api下载文件时从服务器获取文件名

  22. 22

    Web API下载锁定文件

  23. 23

    使用Python通过Google Drive API下载文件时出现问题

  24. 24

    使用内容处置下载文件时出现问题

  25. 25

    下载zip文件ASP MVC时出现问题

  26. 26

    下载文件时出现问题(java)

  27. 27

    将Web抓取到CSV时出现问题[AttributeError:“ str”对象没有属性“ text”

  28. 28

    将mysql数据库与基于Java的Web应用程序链接时出现问题

  29. 29

    将Matlab与Java集成以在Web中进行部署时出现问题

热门标签

归档