我在使用PNP / SP软件包上传和附加到Sharepoint中的列表项时遇到了一些困难。我对输入文件组件没有太多经验,所以我认为我可能缺少在文件上传html元素和将文件提交到SharePoint Web服务之间的步骤。
到目前为止,我已经尝试对PNP示例进行一些更改https://pnp.github.io/pnpjs/sp/docs/attachments/,并尝试了一些不同的参数,但是它们都倾向于导致409或500个错误,其中一个错误提到正在尝试GET请求而不是发布。
我的代码在下面,明天我到办公室时,我会张贴完整的错误消息,但是任何帮助将不胜感激。
private setButtonsEventHandlers(): void {
let fileUpload = document.getElementById("fileUploadInput")
if(fileUpload) {
fileUpload.addEventListener('change', () => {
this.uploadFiles(fileUpload);
});
}
}
private async uploadFiles(fileUpload) {
let file = fileUpload.files[0];
let attachmentsArray = this.state.attachmentsToUpload;
let _web = new Web(this.props.wpContext.pageContext.site.absoluteUrl);
let _listItem;
let listUrlSplit: string[] = this.props.listUrl.split("/");
let listName: string = listUrlSplit[listUrlSplit.length-1];
_listItem = await _web.lists.getByTitle(listName).items.getById(this.props.id);
let attachmentUpload = await _listItem.attachmentFiles.add(file.name,file)
}
我通过用字符串替换文件上传来测试代码(如下),它确实有效,所以我认为我的错误是误解了输入文件元素
let attachmentUpload = await _listItem.attachmentFiles.add("Testfile.txt","This is test content")
先谢谢大家,然后享受星期日剩下的一切;)
安迪
答案 0 :(得分:0)
这是我简单的测试示例,可以正常工作(React框架)。
.tsx组件
<div className={ styles.column }>
<input type='file' id='fileUploadInput' name='myfile'/>
<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>
</div>
webpart.ts
public render(): void {
const element: React.ReactElement<IPnpspUploadAttachementProps > = React.createElement(
PnpspUploadAttachement,
{
description: this.properties.description
}
);
ReactDom.render(element, this.domElement);
this.setButtonsEventHandlers();
}
private setButtonsEventHandlers(): void {
let fileUpload = document.getElementById("fileUploadInput")
if(fileUpload) {
fileUpload.addEventListener('change', () => {
this.uploadFiles(fileUpload);
});
}
}
private async uploadFiles(fileUpload) {
let file = fileUpload.files[0];
//let attachmentsArray = this.state.attachmentsToUpload;
let item = sp.web.lists.getByTitle("MyList").items.getById(15);
item.attachmentFiles.add(file.name,file).then(v => {
console.log(v);
});
//let attachmentUpload = await _listItem.attachmentFiles.add(file.name,file)
}