【问题标题】:open files (pdf,image,) using angular使用角度打开文件(pdf,图像,)
【发布时间】:2020-05-17 03:22:01
【问题描述】:

我的服务器端有一个本地路径,我必须以角度获取它并显示它, 我该怎么做? 如果有人能告诉我路,我会很高兴 我知道我需要在 angular 中使用 blob,但是我从服务器获取/返回哪些参数?

这是我的 Angular 代码

openPDF(){
this.ser.openDPFfile().subscribe((response)=>{
  let file = new Blob([response.byteString], { type: 'application/pdf' });            
  var fileURL = URL.createObjectURL(file);
  window.open(fileURL);
}) }

和我的 C# 代码

  [RoutePrefix("api/files")]
    [Route("download")]
    [HttpGet]
    public string GetStreamFile()
    {
        string fileName = "LabResult.pdf";
        string path = @"C:/Users/" + fileName;
        byte[] a = File.ReadAllBytes(path);
        string s = Convert.ToBase64String(a);
        return s;
    }

我需要添加或更改什么? 请帮我 谢谢:)

【问题讨论】:

    标签: angular api file blob


    【解决方案1】:

    您可以以这种方式显示您的文件,而不是将其转换为 Base64String 并将其返回为 angular,因为这很耗时。

    window.open(
          'file path on the server',
          '_blank',
        );
    

    将您的文件放在 wwwroot 文件夹中的某个位置,然后将此地址发送到 Angular 以像上面一样打开它。

    【讨论】:

    • 嘿,感谢您的反馈...但是我的路径是本地路径只有当路径不是本地路径时,您的方式才有可能也许我错了?如果你能给我解释一下,我会很高兴
    • 我想知道为什么你的文件在 C 盘上?为什么不把它们放在 wwwroot 文件夹中?因为当你在网络服务器上发布它时,你可能无法访问 C 盘。
    【解决方案2】:

    您需要在服务器端代码中使 pdf 可下载。当您向服务器发出请求时,应下载 pdf 文件。然后你应该在你的角度项目中使用 window.open 函数来显示该pdf。旧浏览器无法显示您的 pdf 文件。

    这是你的服务器端

    // this is pseudo code
    res.write(createReadStream('./mypdffile.pdf')
    res.header('Content-Type', 'application/pdf')
    res.header('Content-Length', 1000000) // 1megabyte
    res.header('Content-Disposition', `attachment; myfile.pdf`)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-11-13
      • 2015-01-09
      • 2019-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多