PageOffice 开发者中心 PageOffice 开发者中心
首页
文档
  • 后端Java手册 (opens new window)
  • 后端.netcore手册 (opens new window)
  • 前端JavaScript手册 (opens new window)
下载
购买 (opens new window)
首页
文档
  • 后端Java手册 (opens new window)
  • 后端.netcore手册 (opens new window)
  • 前端JavaScript手册 (opens new window)
下载
购买 (opens new window)
  • 开始

  • 通用控制

    • PageOffice最简单的打开保存文件
    • 磁盘路径打开文档
    • 修改标题栏文本内容
    • 自定义工具条上添加按钮
    • 隐藏标题栏
    • 隐藏Office工具栏
    • 隐藏自定义工具栏
    • 禁止保存
    • 禁止另存为
    • 禁止打印
    • 保存后给前台返回自定义结果
    • 给SaveFilePage指向的地址传参
    • 给SaveFilePage指向的地址传参(Vue)
    • SaveFilePage获取页面Form域
    • POBrowser回调父页面的函数传值
    • 给POBrowser打开的页面传参
    • 另存文件为HTML格式
    • 另存文件为PDF格式
    • 保存数据区域数据同时保存文档
    • 文档打开后触发的事件
    • POBrowser窗口关闭前触发的事件
    • 打开保存数据库中的文件
    • WebCreateNew新建文件
    • 控制POBrowser窗口的位置
    • 打开云对象存储上的文档
      • PageOffice控件铺满整个页面
      • 加盖印章和签字功能
      • Office文档在线预览方案
      • 弹出各种样式的消息框及菜单
      • 实现POBrowser窗口内切换打开不同文件
    • Word

    • Excel

    • PDF

    • FileMaker

    • PPT

    • 更多

    目录

    打开云对象存储上的文档

    # 打开云对象存储上的文档

    注意

    本文中展示的代码均为关键代码,复制粘贴到您的项目中,按照实际的情况,例如文档路径,用户名等做适当修改即可使用。

    某些Web项目中的文档存储采用了云对象存储的方式,比如把Office文档全部保存在阿里云对象存储OSS中,或者文件保存在MinIO,或文件保存在腾讯云COS等对象存储中,那么调用PageOffice在线打开云存储中的Office文件时就需要使用二进制流方式打开。

    首先,编写一个下载文件的后台方法读取云对象存储中的Office文件,比如:openStream;

    然后,把此openStream方法的url地址传递给PageOfficeCtrl对象的webOpen的第一个参数即可打开;

    最后,保存文件时,调用FileSaver对象的getFileBytes()或getFileStream()获取到文件二进制流并保存到云对象存储中。

    以打开阿里云对象存储OSS中的Office文件为例:

    # 后端代码

    1. 编写下载文件的后台方法读取云对象存储(比如:阿里云OSS)中的Office文件:openStream;
      @RequestMapping(value = "openStream", method = RequestMethod.GET)
      public void Openstream(HttpServletRequest request, HttpServletResponse response) 
                                throws SQLException, ClassNotFoundException, IOException {
      
          String fileName = request.getParameter("filename");
          // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
          String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
          // 阿里云账号AccessKey。
          String accessKeyId = "yourAccessKeyId";
          String accessKeySecret = "yourAccessKeySecret";
          // 填写Bucket名称,例如examplebucket。
          String bucketName = "examplebucket";
          // 填写Object完整路径,例如exampledir/exampleobject.docx。Object完整路径中不能包含Bucket名称。
          String objectName = "exampledir/" + fileName;
      
          // 创建OSSClient实例。
          OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
      
          try {
              // 调用ossClient.getObject返回一个OSSObject实例,该实例包含文件内容及文件元信息。
              OSSObject ossObject = ossClient.getObject(bucketName, objectName);
              // 调用ossObject.getObjectContent获取文件输入流,可读取此输入流获取其内容。
              InputStream content = ossObject.getObjectContent();
              if (content != null) {
                  
                  byte[] buffer = new byte[4096];
                  int fileSize = 0;
                  int byteread = 0;
      
                  response.reset();
                  OutputStream outputStream = response.getOutputStream();
      
                  while ((byteread = content.read(buffer)) != -1) {
                      fileSize += byteread; //字节数 文件大小
                      outputStream.write(buffer, 0, byteread);
                  }
      
                  // application/msword, application/x-excel, application/ms-powerpoint, application/pdf
                  response.setContentType("application/msword"); 
                  response.setHeader("Content-Disposition",
                          "attachment; filename=down.doc"); //fileN应该是编码后的(utf-8)
                  response.setContentLength(fileSize);
      
                  outputStream.flush();
                  outputStream.close();
                  outputStream = null;
      
                  // 数据读取完成后,获取的流必须关闭,否则会造成连接泄漏,导致请求无连接可用,程序无法正常工作。
                  content.close();
              }
          } catch (OSSException oe) {
              System.out.println("Caught an OSSException, which means your request made it to OSS, "
                      + "but was rejected with an error response for some reason.");
              System.out.println("Error Message:" + oe.getErrorMessage());
              System.out.println("Error Code:" + oe.getErrorCode());
              System.out.println("Request ID:" + oe.getRequestId());
              System.out.println("Host ID:" + oe.getHostId());
          } catch (ClientException ce) {
              System.out.println("Caught an ClientException, which means the client encountered "
                      + "a serious internal problem while trying to communicate with OSS, "
                      + "such as not being able to access the network.");
              System.out.println("Error Message:" + ce.getMessage());
          } finally {
              if (ossClient != null) {
                  ossClient.shutdown();
              }
          }
      
      }
      
      using Aliyun.OSS;
      using Aliyun.OSS.Common;
      
      public async Task<IActionResult> OpenStream()
      {
          String fileName = Request.Query["filename"];
          // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
          var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
          // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
          var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
          var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
          // 填写Bucket名称,例如examplebucket。
          var bucketName = "examplebucket";
          // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.docx。
          var objectName = "exampledir/" + fileName;
      
          // 创建OssClient实例。
          var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
          var obj = client.GetObject(bucketName, objectName);
      
          // 设置响应内容类型为word的内容类型。
          Response.ContentType = "application/msword";
          Response.Headers["Content-Disposition"] = "attachment; filename=" + Path.GetFileName(objectName);
          // 使用流复制的方式将OSS对象的内容输出到响应流中。
          using (var stream = obj.Content)
          {
              await stream.CopyToAsync(Response.Body);
          }
      
          return new EmptyResult();
      }
      
      // Make sure to add code blocks to your code group
      1. 在后端编写代码调用webOpen方法打开文件之前给SaveFilePage属性赋值(设置好保存时由哪个地址接口负责接收处理控件上传的文件流);
        PageOfficeCtrl poCtrl = new PageOfficeCtrl(request);
        poCtrl.setSaveFilePage("saveStream?filename=exampleobject.docx"); // 设置保存文件的接口地址
        //webOpen的第一个参数支持能够输出下载文件的Url相对地址或者文件在服务器上的磁盘路径两种方式
        //查看详细,请在本站搜索“PageOffice属性或方法中涉及到的URL路径或磁盘路径的说明”
        poCtrl.webOpen("openStream?filename=exampleobject.docx", OpenModeType.docNormalEdit, "张佚名");//打开文件
        
        PageOfficeNetCore.PageOfficeCtrl poCtrl = new PageOfficeNetCore.PageOfficeCtrl(Request);
        poCtrl.SaveFilePage = "SaveStream?filename=exampleobject.docx";
        poCtrl.WebOpen("OpenStream?filename=exampleobject.docx", PageOfficeNetCore.OpenModeType.docNormalEdit, "tom");
        
        // Make sure to add code blocks to your code group

        注意

        对PageOfficeCtrl对象的所有属性赋值或函数调用都必须在WebOpen方法调用之前执行,否则会不生效。

        1. 在SaveFilePage属性指向的地址接口saveStream中,创建FileSaver对象处理文件的保存工作。
          @RequestMapping("saveStream")
          public void saveStream(HttpServletRequest request, HttpServletResponse response) 
                                throws ClassNotFoundException, FileNotFoundException, SQLException {
              FileSaver fs = new FileSaver(request, response);
          
              String fileName = request.getParameter("filename");
              // Endpoint以华东1(杭州)为例,其它Region请按实际情况填写。
              String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
              // 阿里云账号AccessKey。
              String accessKeyId = "yourAccessKeyId";
              String accessKeySecret = "yourAccessKeySecret";
              // 填写Bucket名称,例如examplebucket。
              String bucketName = "examplebucket";
              // 填写Object完整路径,例如exampledir/exampleobject.docx。Object完整路径中不能包含Bucket名称。
              String objectName = "exampledir/" + fileName;
          
              // 创建OSSClient实例。
              OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
          
              try {
                  ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(fs.getFileBytes()));
              } catch (OSSException oe) {
                  System.out.println("Caught an OSSException, which means your request made it to OSS, "
                          + "but was rejected with an error response for some reason.");
                  System.out.println("Error Message:" + oe.getErrorMessage());
                  System.out.println("Error Code:" + oe.getErrorCode());
                  System.out.println("Request ID:" + oe.getRequestId());
                  System.out.println("Host ID:" + oe.getHostId());
              } catch (ClientException ce) {
                  System.out.println("Caught an ClientException, which means the client encountered "
                          + "a serious internal problem while trying to communicate with OSS, "
                          + "such as not being able to access the network.");
                  System.out.println("Error Message:" + ce.getMessage());
              } finally {
                  if (ossClient != null) {
                      ossClient.shutdown();
                  }
              }
          
              fs.close();
          }
          
          public async Task<ActionResult> SaveStream()
          {
              String fileName = Request.Query["filename"];
          
              PageOfficeNetCore.FileSaver fs = new PageOfficeNetCore.FileSaver(Request, Response);
              await fs.LoadAsync();
          
              // yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
              var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
              // 从环境变量中获取访问凭证。运行本代码示例之前,请确保已设置环境变量OSS_ACCESS_KEY_ID和OSS_ACCESS_KEY_SECRET。
              var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
              var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
              // 填写Bucket名称,例如examplebucket。
              var bucketName = "examplebucket";
              // 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.docx。
              var objectName = "exampledir/" + fileName;
          
              // 创建OssClient实例。
              var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
              try
              {
                  byte[] binaryData = fs.FileBytes;
                  MemoryStream requestContent = new MemoryStream(binaryData);
                  // 上传文件。
                  client.PutObject(bucketName, objectName, requestContent);
                  Console.WriteLine("Put object succeeded");
              }
              catch (Exception ex)
              {
                  Console.WriteLine("Put object failed, {0}", ex.Message);
              }
          
              return fs.Close();
          }
          
          // Make sure to add code blocks to your code group

          # 前端代码

          本示例无前端关键代码。

          上次更新: 2025/07/21, 15:21:36
          PageOffice | Copyright © 2013-2026 卓正软件 京ICP备12010902号-2 京公网安备 11010502019270号
          • 跟随系统
          • 浅色模式
          • 深色模式
          • 阅读模式