Lasso Soft Inc. > Home

[ web_response->sendFile ]

Method

[web_response->sendFile] begins a browser download of binary data and allows the developer to control HTTP response headers. This method can be used to serve image files, multimedia files, file archives, or even HTML files. The method will end the current request by calling [abort] as soon as all file data is sent to the client. 

The first parameter of the method can be a file object, or a bytes or string object, or any object supporting ->size and ->sub. This is the data that will be served.

The second parameter indicates the file name as it is downloaded to the web browser.

An optional -Type parameter allows the MIME type of the served file to be specified. The default is to probe the input data to determine the proper mime type. gif, pdf, jpeg and png types are automatically supported. If no type can be determined, application/octet-stream is used.

An optional -Charset parameter allows the character set to be specified for text files, and should be used in conjunction with the -Type parameter.

An optional -Disposition parameter allows the Content-Disposition header to be specified. The default value, 'attachment', will force a file download and usually prompt the user to save the file, instead of displaying the file inline.

The parameters -type, -disposition and -charset are set as header fields.

-skipProbe can be set to false to avoid the data probe in the absence of a -type.

-noAbort can be set to true to avoid the abort normally called by this method after the file is sent.

-monitor can be provided, which is an object that will be invoked as the file is being sent to the client. The -monitor object will be invoked with each chunk of data.

In versions previous to Lasso 9, [file_serve] was used to serve a file via the web browser.

  • Syntax
web_response->sendFile(bytes::trait_each_sub, name::string,
	-type=null,
	-disposition='attachment', 
	-charset='',
	-skipProbe=false,
	-noAbort=false,
	-chunkSize=fcgi_bodyChunkSize,
	-monitor=null)
Examples
  • Beginner

To serve a PDF file:

Use the [web_response->sendFile] method. The following example forces a download and prompts the user to save the file to their computer.

Code

web_response->sendFile(bytes(#mypdf), 'mypdf.pdf')

Result

The user is prompted to save the PDF and download it to their computer.

Serve a CSV file.

Code

local(msg = 'A,B,C\nD,E,F')
web_response->sendFile(#msg, 'TheFile.csv', -type='text/csv')

Result

The file "TheFile.csv" is downloaded by the browser.
Resources
  • Articles

Recent Comments

No Comments found

Please note that periodically LassoSoft will go through the notes and may incorporate information from them into the documentation. Any submission here gives LassoSoft a non-exclusive license and will be made available in various formats to the Lasso community.

LassoSoft Inc. > Home

 

 

©LassoSoft Inc 2015 | Web Development by Treefrog Inc | PrivacyLegal terms and Shipping | Contact LassoSoft