简体   繁体   English

Sharepoint 自定义列表的 SPFx React FileViewer

[英]SPFx React FileViewer for Sharepoint Custom List

I am new to SPFx and React.我是 SPFx 和 React 的新手。 The requirement is to read a Sharepoint Custom List and render the list columns with custom look and feel.要求是读取 Sharepoint 自定义列表并以自定义外观呈现列表列。 This is achieved and read all the attachments (more than one, for a single list item).这是实现并阅读所有附件(多个附件,对于单个列表项)。

How to open all the attachments with React FileViewer (more than one) in browser?如何在浏览器中使用 React FileViewer(多个)打开所有附件

public render(): React.ReactElement<ICirDetailsProps> {
    this._renderListAsync();
    return(

      <div className="${ styles.cirDetails }">
        <div className="${ styles.container }">Circulars<br/>
          <div id="cirDetails" className="${ styles.details}"></div>
        </div>
      </div>
    );
}

attachments.forEach((afile: any) => {
      let fileUrl = hostURL + afile.ServerRelativeUrl;
      let fileExt = this.getFileExtension(afile.ServerRelativeUrl); 
      attFiles += `<FileViewer fileType='${fileExt}' filePath='${afile.ServerRelativeUrl}' /><br/>`;
});
const listContainer: Element = document.querySelector("#cirDetails") as HTMLElement;
listContainer.innerHTML=html;

It shows no error, but the attachments are not opened within browser.它没有显示错误,但附件未在浏览器中打开。 I could see the React FileViewer tags are rendered as HTML Content using F12.我可以看到 React FileViewer 标签使用 F12 呈现为 HTML 内容。 What is the mistake am I doing here?我在这里做错了什么?

Sample demo:示例演示:

import * as React from 'react';
import styles from './MyReactFileViewer.module.scss';
import { IMyReactFileViewerProps } from './IMyReactFileViewerProps';
import { escape } from '@microsoft/sp-lodash-subset';
import FileViewer from 'react-file-viewer';
import * as CSS from 'csstype';
import { sp } from "@pnp/sp";
import { IAttachmentInfo } from "@pnp/sp/attachments";
import { IItem } from "@pnp/sp/items/types";
import "@pnp/sp/webs";
import "@pnp/sp/lists/web";
import "@pnp/sp/items";
import "@pnp/sp/attachments";

interface IPnpstate {
  attachments: any[]
}

export default class MyReactFileViewer extends React.Component<IMyReactFileViewerProps, IPnpstate> {
  constructor(props: IMyReactFileViewerProps, state: IPnpstate) {
    super(props);
    this.state = {
      attachments: []
    };

  }
  public componentDidMount() {
    let item = sp.web.lists.getByTitle("MyList").items.getById(18);
    // get all the attachments
    item.attachmentFiles.get().then((files:IAttachmentInfo[]) => {      
      var attachs=[];
      files.map(file=>{
        var fileType=file.FileName.split('.').pop();
        var fileUrl=file.ServerRelativeUrl;
        attachs.push({"fileType":fileType,"fileUrl":fileUrl});
      })
      console.log(attachs);
      this.setState({attachments:attachs});
    });
  }
  public render(): React.ReactElement<IMyReactFileViewerProps> {
    return (
      <div className={styles.myReactFileViewer}>
        <div className={styles.container}>
          <div className={styles.row}>
            <div className={styles.column}>
              <span className={styles.title}>Welcome to SharePoint!</span>
              <p className={styles.subTitle}>Customize SharePoint experiences using Web Parts.</p>
              <p className={styles.description}>{escape(this.props.description)}</p>
              <a href="https://aka.ms/spfx" className={styles.button}>
                <span className={styles.label}>Learn more</span>
              </a>
              {(this.state.attachments || []).map((item, index) => (
                <div key={item.ID}>
                  <FileViewer
                    fileType={item.fileType}
                    filePath={item.fileUrl}
                  />
                </div>
              ))}
            </div>
          </div>
        </div>
      </div>
    );
  }
}

I used IFrame tag, based on the file types, the iframe renders differently.我使用了 IFrame 标签,根据文件类型,iframe 呈现不同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM