API: upload
Source:
http/upload.ts
Provides a static façade (Upload class) for checking and parsing multipart/form-data HTTP requests, typically used for file uploads.
Usage
import { upload, request, response } from "sdk/http";
if (request.getMethod() === "POST") {
if (upload.isMultipartContent()) {
const fileItems = upload.parseRequest();
for (let i = 0; i < fileItems.size(); i++) {
const fileItem = fileItems.get(i);
const contentType = fileItem.getContentType();
console.log(`Content Type: ${contentType}`);
console.log(`Filename: ${fileItem.getOriginalFilename()}`);
// console.log(`Text: ${fileItem.getText()}`);
response.setContentType(contentType);
response.write(fileItem.getBytesNative());
}
} else {
response.println("The request's content must be 'multipart'");
}
} else if (request.getMethod() === "GET") {
response.println("Use POST request.");
}
response.flush();
response.close();
// -----------
// upload.html
// -----------
<!DOCTYPE html>
<html>
<body>
<form action="/services/js/http-tests/upload/upload.js" method="post" enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" multiple>
<br>
<input type="submit" name="submit" value="Submit">
</form>
</body>
</html>Classes
Upload
The static Upload class provides methods to determine if a request contains
multipart content and to parse that content into file items.
Methods
isMultipartContent
isMultipartContent ():booleanChecks if the current incoming HTTP request contains multipart content
(e.g., from an HTML form withenctype="multipart/form-data").
@returns True if the request is multipart, false otherwise.
parseRequest
parseRequest ():FileItemsParses the incoming multipart request content into a collection of file items.
This operation typically consumes the request body.
@returns A FileItems object representing all parts (files and form fields) of the request.
FileItems
Represents a collection of uploaded file and form field items parsed from a multipart request.
Methods
get
get (index:number):FileItemRetrieves a specific item (file or form field) by its index in the collection.
@param index The zero-based index of the item.
@returns A FileItem object representing the item at the specified index.
size
size ():numberReturns the total number of items (files and form fields) in the collection.
@returns The size of the collection.
FileItem
Represents a single item (either an uploaded file or a regular form field)
within a multipart request.
Methods
getName
getName ():stringFor a file upload, returns the original filename as reported by the client.
For a regular form field, this is typically null or undefined.
@returns The original filename string.
getContentType
getContentType ():stringReturns the MIME type of the uploaded file or content part.
@returns The content type string (e.g., 'image/png', 'text/plain').
isEmpty
isEmpty ():booleanChecks if the uploaded item is empty (e.g., a file upload with zero bytes).
@returns True if the item is empty, false otherwise.
getSize
getSize ():numberReturns the size of the uploaded item in bytes.
@returns The size as a number.
getBytes
getBytes ():any[]Retrieves the content of the file item as a JavaScript array of bytes.
This uses a utility (Bytes.toJavaScriptBytes) to convert the native Java byte array.
@returns An array of bytes (any[]).
getBytesNative
getBytesNative ():any[]Retrieves the content of the file item as the native Java byte array.
@returns The native byte array (any[]).
getText
getText ():stringRetrieves the content of the file item as a string.
Note: This assumes the content is text and may not handle all encodings correctly.
It relies on JavaScript'sString.fromCharCode.applyfor conversion.
@returns The content as a string.
getInputStream
getInputStream ():InputStreamGets an input stream for reading the content of the file item.
This is useful for handling large files without loading the entire content into memory.
@returns An InputStream object wrapping the native input stream.