Data
Files and binary content
A Data value is bytes passed around as one value: Http.get(url).data and file.read('data') produce one, and file.write, QuickLook.present, ShareSheet.present, Image.from, an Http body and a multipart upload all take one. The bytes stay in the app until you convert them, so moving a large file between APIs costs nothing. A Data value lasts for one run: save it with file.write to keep it. Storage and Cache save it as base64 and give it back as Data.
Properties
typeStringAlways 'data'.
mimeTypeStringWhat the bytes are, such as 'application/pdf' or 'image/png'.
sizeNumberHow many bytes.
Methods
Data.fromBase64(base64, mimeType?)Data ›A Data value from base64. The type defaults to application/octet-stream.
- base64 String required
- mimeType String
Data.fromText(text, mimeType?)Data ›A Data value holding a string as UTF-8. The type defaults to text/plain.
- text String required
- mimeType String
Data.toBase64(data)StringThe bytes as base64. Copies them into the script, a third larger, so convert only when something needs a string.
- data Data required
Examples
Download a PDF and open it
var pdf = Http.get("https://example.com/menu.pdf").data;
FileSystem.local.file("menu.pdf").write(pdf);
QuickLook.present(pdf);Check before using
var file = Http.get(url).data;
if (file.mimeType !== "application/pdf" || file.size > 10e6) {
console.warn("Not a PDF, or too large");
}Upload a file
var csv = FileSystem.local.file("export.csv").read("data");
Http.post(uploadUrl, { multipart: [
{ name: "file", value: csv, filename: "export.csv" },
]});Send it inside JSON
Http.post(apiUrl, { body: {
name: "logo.png",
content: Data.toBase64(logo),
}});