upload
An upload file method that can be used to upload a file and go through the attachment field writing.
Parameters
Name | Type | Description |
---|---|---|
params | Object | - |
params.file | File | Files to be uploaded. |
params.datasheetId | string | Upload files to datasheet. |
params.onProgress? | (response : IUploadProgress ) => void | File upload progress callback method. |
Returns
Promise
<IAttachmentValue
>
Example
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>
)
}