跳到主要内容

upload

一个上传文件方法,可以使用此方法上传文件,去完成附件字段的写入

参数

NameTypeDescription
paramsObject-
params.fileFile文件
params.datasheetIdstring维格表ID
params.onProgress?(response: IUploadProgress) => void文件上传进度回调方法

返回值

Promise<IAttachmentValue>

示例

import React, { useState } from 'react';
import { upload, useDatasheet } from '@apitable/widget-sdk';

function UploadFile() {
const datasheet = useDatasheet();
const [progress, setProgress] = useState(0);
const [error, setError] = useState();
function uploadFile(file) {
datasheet && upload({
file,
datasheetId: datasheet?.datasheetId,
onProgress: ({ loaded, total }) => {
setProgress(loaded / total);
}
}).then(response => {
const valuesMap = { fldGi8tYQfXcc: [response] };
const permission = datasheet.checkPermissionsForAddRecord(valuesMap);
if (permission.acceptable) {
datasheet.addRecord(valuesMap);
} else {
setError(permission.message);
}
});
}
return (
<div>
<input type="file" onChange={(e) => {
e.target.files?.[0] && uploadFile(e.target.files[0])
}}/>
<p>progress: {progress}</p>
{ error && <p>error: {error}</p>}
</div>
)
}