attachMedia
A method that allows you to upload a file to the application: video, image, audio, and any other file.
import { attachMedia } from "@app/ui"
attachMedia({ submitUrl, title, mediaType })
Arguments
params*: { title, mediaType, submitUrl }
An object with upload parameters.
-
submitUrl*:
string- The API endpoint where information about the uploaded file will be sent. File information will be available to the handler in req.body.file , specifically:
-
req.body.file.hash:
stringThe identifier of the file in the application's storage.
-
req.body.file.name:
stringThe original name of the file before uploading to the application.
-
title:
stringThe title of the file selection dialog. This can help guide the user on what type of file they are selecting.
-
mediaType:
photo | video | undefined-
Default: undefined
Allows you to restrict the types of files permitted for upload.
photo— restricts file types to images. On mobile devices, it prompts the user to select an image from the gallery.video— restricts file types to videos. On mobile devices, it prompts the user to select a video from the gallery.undefined— does not restrict the file type. On mobile devices with this option, images and videos located in the gallery may be unavailable.
-
Examples
When the button is pressed, it prompts to upload an image. After uploading, the image is displayed on the screen.
import { attachMedia } from '@app/ui'
import { getThumbnailUrl } from '@app/storage'
app.apiCall('onImageUpload', async (ctx, req) => {
return ctx.router.navigate('imageUpload?hash=' + req.body.file.hash)
})
app.screen('imageUpload', async (ctx, req) => {
const onButtonClick = attachMedia({
title: "Choose an image",
mediaType: "photo",
submitUrl: ctx.router.url('onImageUpload')
})
return (
<screen>
<button title="Upload an image" onClick={onButtonClick} />
{req.query?.hash && <img src={getThumbnailUrl(req.query.hash)} />}
</screen>
)
})