ServerResponse Write File Method in SuiteScript
The ServerResponse writeFile method in SuiteScript allows server scripts to write files to HTTP responses, facilitating file downloads.
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:
| Parameter | Type | Required / Optional | Description |
|---|---|---|---|
options.file | file.File | Required | A file.File object to write to the response. |
options.isInline | boolean | Optional | If 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 requiredoptions.fileparameter is missing.WRONG_PARAMETER_TYPE: Raised when the providedoptions.fileis not a validfile.Fileobject.
Usage Example
Here’s a basic example of how to use the writeFile method within a SuiteScript:
1// Sample code to write a file response2var serverResponse; // Assume this is your instance of ServerResponse3var myFileObj = file.load({ id: '12345' }); // Load your file object4 5serverResponse.writeFile({6 file: myFileObj,7 isInline: true8});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
- http.ServerResponse
- N/http Module
- SuiteScript 2.x Modules
Key Takeaways
- The
writeFilemethod handles file outputs in server scripts, allowing dynamic file downloads. - It's vital to ensure the file is loaded as a
file.Fileobject 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?
Is there a governance limit for using the writeFile method in SuiteScript?
What errors might occur if the required parameters for writeFile are not provided correctly?
How do you specify a file to write to the response using the writeFile method?
Was this article helpful?
More in SuiteScript
- Common SuiteScript Errors and Solutions for NetSuite
Common NetSuite script errors include INVALID_SCRIPT_DEPLOYMENT_ID and SSS_AUTHORIZATION_HEADER_NOT_ALLOWED. Learn effective solutions.
- Set Sublist Field Values in SuiteScript 2.x for Record Management
Set sublist field values in SuiteScript 2.x for effective record management using standard and dynamic modes.
- Setting Field Values in SuiteScript for Effective Record
Learn to set field values in SuiteScript effectively, troubleshooting common errors and understanding data types.
- SuiteScript 2.1 Enhancements and API Updates in NetSuite
SuiteScript 2.1 enables execution of 2.0 scripts and supports PATCH method for enhanced API capabilities.
Advertising
Reach SuiteScript Professionals
Put your product in front of NetSuite experts who work with SuiteScript every day.
Sponsor This Category