Wt: Wt::WFileResource Class Reference

Wt 3.3.0

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes

- Class List
- Class Hierarchy
- Class Members

Public Member Functions
Wt::WFileResource Class Reference

A resource which streams the data from a local file. More...

`#include <Wt/WFileResource>`

Inheritance diagram for Wt::WFileResource:
[legend]

List of all members.

## Public Member Functions
| WFileResource (WObject *parent=0)
| Default constructor.
| WFileResource (const std::string &fileName, WObject *parent=0)
| Creates a new resource for a file.
| WFileResource (const std::string &mimeType, const std::string &fileName, WObject *parent=0)
| Creates a new resource with given mime-type for a file.
| ~WFileResource ()
| Destructor.
void | setFileName (const std::string &fileName)
| Sets a (different) filename.
const std::string & | fileName () const
| Returns the filename.
virtual void | handleRequest (const Http::Request &request, Http::Response &response)
| Handles a request.

## Detailed Description

A resource which streams the data from a local file.

To update the resource, either use setFileName() to point it to a new file, or emit the WResource::dataChanged() signal when only the file contents has changed, but not the filename.

The resource makes use of continuations to transmit data piecewise, without blocking a thread or requiring the entire file to be read in memory. The size of the buffer can be changed using setBufferSize().

Usage examples:

     Wt::WFileResource *csvFile = new Wt::WFileResource("text/csv", "/opt/files/afile.csv");
     csvFile->suggestFileName("data.csv");
     Wt::WAnchor *anchor = new Wt::WAnchor(csvFile, "CSV data");

     Wt::WFileResource *imageFile = new Wt::WFileResource("image/png", "/opt/files/image.png");
     imageFile->suggestFileName("data.png");
     Wt::WImage *image = new Wt::WImage(imageFile, "PNG version");

See also:
  WStreamResource, WMemoryResource

## Constructor & Destructor Documentation

Wt::WFileResource::WFileResource | ( | WObject * | parent = `0` | ) |

Default constructor.

You need to set a file name (and mime type) for the resource using setFileName() and setMimeType().

Wt::WFileResource::WFileResource | ( | const std::string & | fileName,
|  | WObject * | parent = `0`
| ) |  |

Creates a new resource for a file.

The mime type defaults to "text/plain".

Wt::WFileResource::~WFileResource | ( |  | ) |

Destructor.

It is up to the user to make sure that the resource is no longer in use (by e.g. a WImage).

## Member Function Documentation

void Wt::WFileResource::handleRequest | ( | const Http::Request & | request,
|  | Http::Response & | response
| ) |  | `[virtual]`

Handles a request.

You may want to specialize this function to compute the file on the fly. However, you need to take into account the fact that the WFileResource implementation may use continuations to split the download in smaller chunks. Your implementation should thus look like:

     void handleRequest(const Http::Request& request,
                        Http::Response& response) {
       if (!request.continuation()) {
         ... prepare data
         setFileName(myTmpFile);
       }

       WFileResource::handleRequest(request, response);
     }

Implements Wt::WResource.

void Wt::WFileResource::setFileName | ( | const std::string & | fileName | ) |

Sets a (different) filename.

Set the location of the file on the local filesystem which must be streamed for this resource.

All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator