问题
I am having some difficulty upload and attachment to a list item in sharepoint using the PNP/SP package. I dont have much experience with the input file component so I think I may be missing a step between the file upload html element and submitting the file to the SharePoint web service.
So far I've tried to follewing the PNP example with a few changes https://pnp.github.io/pnpjs/sp/docs/attachments/ and tried a few different arguments but they all tend to result in 409 or 500 errors, one error mentions that it's attempting a GET request instead of post.
My code is below and i'll post full error messages when i get into the office tomorrow but any help would be greatly appreciated.
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)
}
I tested the code (below) by replacing my file upload with strings and it does work so I think my error is in misunderstanding the input file element
let attachmentUpload = await _listItem.attachmentFiles.add("Testfile.txt","This is test content")
Thanks in advance all and enjoy whats left of sunday ;)
Andy
回答1:
Here is my simple test demo which works(React framework).
Component .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)
}
来源:https://stackoverflow.com/questions/55677531/spfx-uploading-and-adding-attachment-to-a-list