我是SPFx和React的新手。要求是读取Sharepoint自定义列表,并以自定义外观呈现列表列。这可以实现并读取所有附件(对于单个列表项,不止一个)。
如何如何使用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;
它没有显示错误,但未在浏览器中打开附件。我可以看到React FileViewer标签使用F12呈现为HTML内容。我在这里做什么错?
示例演示:
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>
);
}
}