Wt  4.11.1
Public Member Functions | List of all members
Wt::WFileResource Class Reference

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

#include <Wt/WFileResource.h>

Inheritance diagram for Wt::WFileResource:
[legend]

Public Member Functions

 WFileResource ()
 Default constructor. More...
 
 WFileResource (const std::string &fileName)
 Creates a new resource for a file. More...
 
 WFileResource (const std::string &mimeType, const std::string &fileName)
 Creates a new resource with given mime-type for a file.
 
 ~WFileResource ()
 Destructor. More...
 
void setFileName (const std::string &fileName)
 Sets a (different) filename. More...
 
const std::string & fileName () const
 Returns the filename.
 
virtual void handleRequest (const Http::Request &request, Http::Response &response) override
 Handles a request. More...
 
- Public Member Functions inherited from Wt::WStreamResource
 WStreamResource ()
 Default constructor. More...
 
 WStreamResource (const std::string &mimeType)
 Creates a new resource with given mime-type.
 
 ~WStreamResource ()
 Destructor. More...
 
void setMimeType (const std::string &mimeType)
 Sets the mime-type.
 
const std::string & mimeType () const
 Returns the mime-type.
 
void setBufferSize (int size)
 Configures the buffer size. More...
 
int bufferSize () const
 Returns the buffer size. More...
 
- Public Member Functions inherited from Wt::WResource
 WResource ()
 Creates a new resource.
 
 ~WResource ()
 Destroys the resource. More...
 
void suggestFileName (const Wt::WString &name, ContentDisposition disposition=ContentDisposition::Attachment)
 Suggests a filename to the user for the data streamed by this resource. More...
 
const Wt::WStringsuggestedFileName () const
 Returns the suggested file name. More...
 
void setDispositionType (ContentDisposition cd)
 Configures the Content-Disposition header. More...
 
ContentDisposition dispositionType () const
 Returns the currently configured content disposition. More...
 
void setChanged ()
 Generates a new URL for this resource and emits the changed signal. More...
 
void setInvalidAfterChanged (bool enabled)
 Return "page not found" for prior resource URLs after change. More...
 
bool invalidAfterChanged () const
 Should "page not found" be returned for outdated resource URLs. More...
 
void setInternalPath (const std::string &path)
 Sets an internal path for this resource. More...
 
std::string internalPath () const
 Returns the internal path. More...
 
const std::string & generateUrl ()
 Generates an URL for this resource. More...
 
const std::string & url () const
 Returns the current URL for this resource. More...
 
SignaldataChanged ()
 Signal emitted when the data presented in this resource has changed. More...
 
void setUploadProgress (bool enabled)
 Indicate interest in upload progress. More...
 
Signal< ::uint64_t, ::uint64_t > & dataReceived ()
 Signal emitted when data has been received for this resource. More...
 
void write (std::ostream &out, const Http::ParameterMap &parameters=Http::ParameterMap(), const Http::UploadedFileMap &files=Http::UploadedFileMap())
 Stream the resource to a stream. More...
 
virtual void handleAbort (const Http::Request &request)
 Handles a continued request being aborted. More...
 
void haveMoreData ()
 Indicate that more data is available. More...
 
void setTakesUpdateLock (bool enabled)
 Set whether this resource takes the WApplication's update lock. More...
 
bool takesUpdateLock () const
 Returns whether this resources takes the WApplication's update lock. More...
 
- Public Member Functions inherited from Wt::WObject
void addChild (std::unique_ptr< WObject > child)
 Add a child WObject whose lifetime is determined by this WObject.
 
template<typename Child >
Child * addChild (std::unique_ptr< Child > child)
 Add a child WObject, returning a raw pointer. More...
 
std::unique_ptr< WObjectremoveChild (WObject *child)
 Remove a child WObject, so its lifetime is no longer determined by this WObject.
 
template<typename Child >
std::unique_ptr< Child > removeChild (Child *child)
 Remove a child WObject, so its lifetime is no longer determined by this WObject. More...
 
virtual const std::string id () const
 Returns the (unique) identifier for this object. More...
 
virtual void setObjectName (const std::string &name)
 Sets an object name. More...
 
virtual std::string objectName () const
 Returns the object name. More...
 
void resetLearnedSlots ()
 Resets learned stateless slot implementations. More...
 
template<class T >
void resetLearnedSlot (void(T::*method)())
 Resets a learned stateless slot implementation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)())
 Declares a slot to be stateless and learn client-side behaviour on first invocation. More...
 
template<class T >
WStatelessSlot * implementStateless (void(T::*method)(), void(T::*undoMethod)())
 Declares a slot to be stateless and learn client-side behaviour in advance. More...
 
void isNotStateless ()
 Marks the current function as not stateless. More...
 
template<class T >
WStatelessSlot * implementJavaScript (void(T::*method)(), const std::string &jsCode)
 Provides a JavaScript implementation for a method. More...
 
- Public Member Functions inherited from Wt::Core::observable
 observable () noexcept
 Default constructor.
 
virtual ~observable ()
 Destructor. More...
 
template<typename... Args, typename C >
auto bindSafe (void(C::*method)(Args...)) noexcept
 Protects a method call against object destruction. More...
 
template<typename... Args, typename C >
auto bindSafe (void(C::*method)(Args...) const) const noexcept
 Protects a const method call against object destruction. More...
 
template<typename Function >
auto bindSafe (const Function &function) noexcept
 Protects a function against object destruction. More...
 

Additional Inherited Members

- Public Types inherited from Wt::WObject
typedef void(WObject::* Method) ()
 Typedef for a WObject method without arguments.
 
- Protected Member Functions inherited from Wt::WStreamResource
void handleRequestPiecewise (const Http::Request &request, Http::Response &response, std::istream &input)
 Handles a request and streams the data from a std::istream. More...
 
- Protected Member Functions inherited from Wt::WResource
void beingDeleted ()
 Prepares the resource for deletion. More...
 
- Protected Member Functions inherited from Wt::WObject
virtual WStatelessSlot * getStateless (Method method)
 On-demand stateless slot implementation. More...
 

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:

auto csvFile = std::make_unique<Wt::WFileResource>("text/csv", "/opt/files/afile.csv");
csvFile->suggestFileName("data.csv");
auto anchor = std::make_unique<Wt::WAnchor>(csvFile.get(), "CSV data");
auto imageFile = std::make_unique<Wt::WFileResource>("image/png", "/opt/files/image.png");
imageFile->suggestFileName("data.png");
auto image = std::make_unique<Wt::WImage>(imageFile.get(), "PNG version");
See also
WStreamResource, WMemoryResource

Constructor & Destructor Documentation

◆ WFileResource() [1/2]

Wt::WFileResource::WFileResource ( )

Default constructor.

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

◆ WFileResource() [2/2]

Wt::WFileResource::WFileResource ( const std::string &  fileName)

Creates a new resource for a file.

The mime type defaults to "text/plain".

◆ ~WFileResource()

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

◆ handleRequest()

void Wt::WFileResource::handleRequest ( const Http::Request request,
Http::Response response 
)
overridevirtual

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);
}
void setFileName(const std::string &fileName)
Sets a (different) filename.
Definition: WFileResource.C:34
virtual void handleRequest(const Http::Request &request, Http::Response &response) override
Handles a request.
Definition: WFileResource.C:40

Implements Wt::WResource.

◆ setFileName()

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.