Skip to content

Commit

Permalink
Merge pull request #2682 from openedx/mkeating/dropzone-example-filename
Browse files Browse the repository at this point in the history
chore: adding file name to in-memory Dropzone upload example
  • Loading branch information
marlonkeating authored Oct 4, 2023
2 parents 5f3ce87 + ae878d6 commit 555bc08
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/Dropzone/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,25 +280,21 @@ This example validates that only `400x479` images can be uploaded.

## Reading file contents into memory

Accepts only .xml files up to a size of 20MB. You can read in the contents of the `File` object into memory. The ``onProcessUpload`` prop should retrieve the file Blob from the passed ``fileData`` param and pass it into a file reader.
Accepts only .xml files up to a size of 20MB. You can read in the contents of the `File` object into memory. The ``onProcessUpload`` prop can retrieve the file Blob from the passed ``fileData`` param and either pass it into a file reader or use text() promise.

Note that `Dropzone` does not handle unexpected errors that might happen in your function, they should be handled by the ``handleProcessUpload`` method.

```jsx live
() => {
const [text, setText] = useState("");
const [fileName, setFileName] = useState("");

async function handleProcessUpload({
fileData, requestConfig, handleError
}) {
const handleProcessUpload = ({ fileData }) => {
const blob = fileData.get('file');
const reader = new FileReader();
reader.readAsDataURL(blob);
reader.onloadend = function() {
const base64data = reader.result.split(',')[1];
console.log(atob(base64data));
setText(atob(base64data));
}
blob.text().then(xmlText => {
setText(xmlText);
setFileName(blob.name);
});
};

return (
Expand All @@ -313,6 +309,12 @@ Note that `Dropzone` does not handle unexpected errors that might happen in your
"application/xml": ['.xml']
}}
/>
{fileName && (
<Form.Control.Feedback type="valid">
Uploaded{' '}
{fileName}
</Form.Control.Feedback>
)}
<p>{text}</p>
</>
)
Expand Down

0 comments on commit 555bc08

Please sign in to comment.