ServerResponse Write File Method in SuiteScript

The ServerResponse writeFile method in SuiteScript allows server scripts to write files to HTTP responses, facilitating file downloads.

·2 min read·View Oracle Docs

The writeFile method of the ServerResponse object in SuiteScript enables developers to write files to HTTP responses. This is useful when building Suitelets or server-side scripts that need to output files for download or inline display to users.

Method Overview

  • Method Description: Writes a file to the response without returning any value.
  • Supported Script Types: This method is compatible with server scripts.
  • Governance: There are no governance limits associated with this method.

Parameters

The method accepts an options parameter, which is a JavaScript object comprising the following key properties:

ParameterTypeRequired / OptionalDescription
options.filefile.FileRequiredA file.File object to write to the response.
options.isInlinebooleanOptionalIf true, the file is presented inline; defaults to false.

Note: The options.file parameter must be specified as a file.File object, which you can create using the file module.

Error Handling

Several error codes may be thrown by the writeFile method:

  • SSS_MISSING_REQD_ARGUMENT: Thrown if the required options.file parameter is missing.
  • WRONG_PARAMETER_TYPE: Raised when the provided options.file is not a valid file.File object.

Usage Example

Here’s a basic example of how to use the writeFile method within a SuiteScript:

javascript
1// Sample code to write a file response
2var serverResponse; // Assume this is your instance of ServerResponse
3var myFileObj = file.load({ id: '12345' }); // Load your file object
4
5serverResponse.writeFile({
6 file: myFileObj,
7 isInline: true
8});

This code demonstrates how to load a file and then write it to the response, which can be utilized in scenarios like file downloads or displaying files directly in a browser window.

Related Topics

Key Takeaways

  • The writeFile method handles file outputs in server scripts, allowing dynamic file downloads.
  • It's vital to ensure the file is loaded as a file.File object before using it.
  • Error handling is important to manage required parameters and data types effectively.

Frequently Asked Questions (4)

What script types support the ServerResponse writeFile method?
The writeFile method is compatible with server scripts in SuiteScript.
Is there a governance limit for using the writeFile method in SuiteScript?
No, there are no governance limits associated with the writeFile method in SuiteScript.
What errors might occur if the required parameters for writeFile are not provided correctly?
If the required options.file parameter is missing, the SSS_MISSING_REQD_ARGUMENT error is thrown. If an incorrect data type is provided, the WRONG_PARAMETER_TYPE error may occur.
How do you specify a file to write to the response using the writeFile method?
You must specify the file as a file.File object using the options.file parameter when calling the writeFile method.
Source: Parameters Oracle NetSuite Help Center. This article was generated from official Oracle documentation and enriched with additional context and best practices.

Was this article helpful?

More in SuiteScript

View all SuiteScript articles →