This code extracts the text file associated with the "textfile" field from the XML request and saves it on the QIE computer.
// 1. Get the content-disposition for the textfile field using XPath
var contentDisposition = source.getNode("//Part[content-disposition[contains(., 'name=\"textfile\"')]]/content-disposition");
// 2. Define the pattern to match the filename
var pattern = java.util.regex.Pattern.compile("filename=\"([^\"]+)\"");
// 3. Match the pattern against the text in the content-disposition tag
var matcher = pattern.matcher(contentDisposition);
var filename;
// 4. Check if the pattern is found
if (matcher.find()) {
// 5. Extract the filename from the matched group
filename = matcher.group(1);
}
// 6. Get the content for the file using XPath
var fileContent = source.getNode("//Part[content-disposition[contains(., 'name=\"textfile\"')]]/Content");
// 7. Base64 decode the content to a string
var fileText = qie.base64Decode(fileContent);
// 8. Write the byte[] to a file on disk
qie.writeFile('/Users/dir/Pictures/' + filename, fileText);
1. This line fetches the content-disposition for the "textfile" part in the XML request using XPath. It looks for a Part
element where the content-disposition
contains the substring 'name="textfile"'
, then extracts the content-disposition
value from it.
2. Here, a regular expression pattern is defined to match the filename within a content-disposition tag. This pattern looks for the substring 'filename="...'
, capturing everything within double quotes after the equals sign.
3. This line creates a matcher object by applying the defined regular expression pattern to the content-disposition obtained in the first step. The matcher will be used to find the filename within the content-disposition.
4. This conditional statement checks if the matcher finds a match according to the pattern. If a match is found, the following block of code is executed.
5. If a match is found, this line extracts the filename from the matched group (the portion captured within parentheses in the regular expression pattern) and assigns it to the filename
variable.
6. Similar to the first line, this fetches the content of the file from the XML request using XPath. It looks for a Part
element where the content-disposition
contains the substring 'name="textfile"'
, then extracts the Content
from it.
7. The content fetched in the previous step is assumed to be Base64 encoded. This line decodes the Base64-encoded content to a string.
8. Finally, this line writes the files text obtained from decoding the file content to a file on disk. The file is written to the directory specified ('/Users/dir/Pictures/') with the filename extracted earlier.
See also:
Extracting Form Fields from multipart/form-data requests
Extracting Base64 Encoded Binary File from File Field in multipart/form-data requests
Handling HTTP requests and responses
Processing Requests in mapping nodes via HTTP Listener source node