Repository navigation
Multipart Data Utilities
This sets up a utility factory for generating the buffer body for a multipart/form-data request and its associated request headers. The data can be a struct or a JsonStruct. Example of usage:
var mpdb = new MultipartDataBuilder({
title: "Twerking Turtle",
description: "You know what to do with that big fat butt?",
image: new FilePart(working_directory + "twerking_turtle.gif")
});
var bufferBody = mpdb.getBuffer();
var headers = mpdb.getHeaderMap();
The example above contains 2 keys containing plain strings and 1 key containing a file attachment part.
-
boundary: The boundary fragment used to separate parts. Is automatically generated, but can be manually set before callinggetBuffer(). -
data: The struct being encoded intomultipart/form-data. -
size: The length of the resulting request buffer. Only available aftergetBuffer()is called.
-
getBuffer(): Encode the struct data intomultipart/form-dataand return the resulting buffer. -
getHeaderMap(): Return a map containing the prerequisite headers for the encoded struct request body. Use only aftergetBuffer()is called! -
writeHeaderMap(map): Write the prerequisite headers for the encoded struct request body to an existing map. Use only aftergetBuffer()is called!
This represents a binary file attachment emulated by a buffer. Example:
new BufferFilePart("upload.bin", buffer)
-
buffer: The buffer representing the emulated file's body. -
bufferStart: The position in the buffer where the emulated file's body starts. Default: 0 -
bufferSize: The length of the buffer to use as the emulated file's body. Default:buffer_tell(buffer) -
filename: The name of the emulated file. -
mimeType: The MIME type of the emulated file. Is automatically computed using the extension onfilename, but can be manually specified if desired.
-
writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use byMultipartDataBuilder.getBuffer().
This represents a normal part containing data from a buffer. Example:
new BufferPart(binData)
-
buffer: The buffer representing the part body. -
bufferStart: The position in the buffer where the part's body starts. Default: 0 -
bufferSize: The length of the buffer to use as the part's body. Default:buffer_tell(buffer)
-
writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use byMultipartDataBuilder.getBuffer().
This represents a binary file attachment. Example:
new FilePart(working_directory + "twerking_turtle.gif")
On VM and YYC exports, this type can be used for both binary and text files. On HTML5, this type can only be used for emulated files saved using buffer-saving functions (which include files downloaded using xhr_download with textMode off).
-
filepath: The given path to the attached file. -
mimeType: The MIME type of the file. Is automatically computed based on the extension onfilepath, but can be manually specified if desired.
-
writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use byMultipartDataBuilder.getBuffer().
This represents a text file attachment emulated by a string. Example:
new StringFilePart("example.txt", "Hello World!\nGoodbye World!")
-
body: The body of the emulated file. -
filename: The name of the emulated file. -
mimeType: The MIME type of the emulated file. Is automatically computed using the extension onfilename, but can be manually specified if desired.
-
writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use byMultipartDataBuilder.getBuffer().
This represents a text file attachment from an actual file. Example:
new TextFilePart(working_directory + "readme.md")
-
filepath: The given path to the attached file. -
mimeType: The MIME type of the file. Is automatically computed based on the extension onfilepath, but can be manually specified if desired. -
newline: The newline used in the file. Default:"\r\n" -
trailingNewline: Whether to end the file with a newline. Default:false
newline and trailingNewline can be optionally be set in the constructor using a struct passed as a parameter:
new TextFilePart(working_directory + "level1.data", { newline: "\n", trailingNewline: true })
-
writeToMultipartBuffer(buffer, key, boundary): Write to the buffer with the given key and part boundary. For use byMultipartDataBuilder.getBuffer().