我正在我们的SharePoint Online租户内构建一个SPFx web部件,在那里我们有一个react dropzone,上传一个文件,它应该自动上传到SharePoint文档库:-
这是我的服务:-
// Uploads a dropped excel sheet to the site assets library
public uploadExcel = async (name:string, file: ArrayBuffer): Promise<string> => {
try {
alert("1");
const fileAddResult = await this.dmsWeb.lists
.getByTitle(this.folderIconListTitle)
.rootFolder.files.addUsingPath(name,file, { Overwrite: true });
return fileAddResult.data.ServerRelativeUrl;
} catch (ex) {
const result = (ex as Error).message;
console.log(result);
Dialog.alert(result);
}
};
这是我的标记:-
public render(): React.ReactElement<ICategoriesPanelProps> {
const appearingStyle = mergeStyles(AnimationStyles.scaleDownIn100);
//code goes here
<Stack tokens={{ childrenGap: 15 }}>
<Stack.Item>
<DropzoneExport
themeVariant={this.props.themeVariant}
onDrop={this.onDrop}
uploadPlaceholders={this.state.uploadPlaceholders}
removeDocument={this.removeDocument}
/>
下面是onDrop方法:-
private onDrop = (acceptedFiles) => {
try{
acceptedFiles.forEach(file => {
const reader = new FileReader()
reader.onabort = () => console.log('file reading was aborted')
reader.onerror = () => console.log('file reading has failed')
reader.onload = async () => {
// get file content
const binaryStr = reader.result
this.props.uploadExcel("Category", binaryStr);
}}}
catch(e)
{const result = (e as Error).message;
alert(result);}
};
目前我得到这个错误:-
类型为'string | ArrayBuffer'的实参不能赋值给参数类型为'ArrayBuffer'。类型'string'不能赋值给Type"ArrayBuffer"。
onthis.props.uploadExcel("Category", binaryStr);
。任何建议吗?由于
问题是FileReader.result
可以是string
或ArrayBuffer
(在打字术语中,它是string | ArrayBuffer
,即"string或ArrayBuffer")。你的函数uploadExcel
被声明为只接受ArrayBuffer
作为参数。
您可能需要使用一些类型强制转换。最简单的方法(如果您确定binaryStr实际上包含ArrayBuffer而不是字符串):
this.props.uploadExcel("Category", binaryStr as ArrayBuffer);
除此之外,看起来您实际上需要读取(例如,调用FileReader
的读方法之一,如reader.readAsArrayBuffer(file)
)。现在,变量'file'似乎没有使用。