Developers use Treefrog to build a project, generate a dynamic library, will be tfserver loaded, tfserver URL processing as controller, action, argument three parts, reference URL Routing this document. as follows:
[HTML]View Plaincopy
- /controller-name/action-name/argument1/argument2/...
Corresponds to our fileserver this project, Controller-name is Fileserver, action-name is files, argument1 is the specific file name. Use this address when accessing the file: Http://localhost:8800/fileserver/files/xxx.
We simply retrofit the previous HelloWorld example to get an HTTP file server.
The header files are as follows:
[CPP]View Plaincopy
- #ifndef Fileservercontroller_h
- #define Fileservercontroller_h
- #include "Applicationcontroller.h"
- Class T_controller_export Fileservercontroller: Public Applicationcontroller
- {
- Q_object
- Public
- Fileservercontroller () {}
- Fileservercontroller (const fileservercontroller &other);
- Public Slots:
- void index ();
- void files ();
- void Files (const QString ¶m);
- };
- T_declare_controller (Fileservercontroller, Fileservercontroller);
- #endif//Fileservercontroller_h
In the code above, public slots: The following section is action. When tfserver resolves the URL, it is called to the action. We have added two slots named files.
Here is the source file:
[CPP]View Plaincopy
- #include "Fileservercontroller.h"
- Fileservercontroller::fileservercontroller (const fileservercontroller &other)
- : Applicationcontroller ()
- {}
- void Fileservercontroller::index ()
- {
- RenderText ("Denied");
- }
- void Fileservercontroller::files ()
- {
- RenderText ("Invalid parameter");
- }
- void Fileservercontroller::files (const QString ¶m)
- {
- SendFile (param, "Application/octet-stream", " " ");
- }
- T_register_controller (Fileservercontroller);
In the implementation of files, we simply call SendFile to send the file. In fact, Trace SendFile will find that this function is simply to locate the file and open it and assign a Qiodevice object pointer to the Bodydevice member of Thttpresponse. The following will use this bodydevice in Tactionthread to do the actual data sending action. When the file is opened, Param is looked up as the file name in the root directory of the Web site (the project root directory is in the example).
Now, we can test the download by Http://localhost:8800/fileserver/files/appbase.pri this URL. I work here normally.
HTTP file server developed by the Treefrog (c + + Web Framework)