HTTP file server developed by the Treefrog (c + + Web Framework)

Source: Internet
Author: User
Tags sendfile

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
    1. /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
  1. #ifndef Fileservercontroller_h
  2. #define Fileservercontroller_h
  3. #include "Applicationcontroller.h"
  4. Class T_controller_export Fileservercontroller: Public Applicationcontroller
  5. {
  6. Q_object
  7. Public
  8. Fileservercontroller () {}
  9. Fileservercontroller (const fileservercontroller &other);
  10. Public Slots:
  11. void index ();
  12. void files ();
  13. void Files (const QString &param);
  14. };
  15. T_declare_controller (Fileservercontroller, Fileservercontroller);
  16. #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
  1. #include "Fileservercontroller.h"
  2. Fileservercontroller::fileservercontroller (const fileservercontroller &other)
  3. : Applicationcontroller ()
  4. {}
  5. void Fileservercontroller::index ()
  6. {
  7. RenderText ("Denied");
  8. }
  9. void Fileservercontroller::files ()
  10. {
  11. RenderText ("Invalid parameter");
  12. }
  13. void Fileservercontroller::files (const QString &param)
  14. {
  15. SendFile (param, "Application/octet-stream", " " ");
  16. }
  17. 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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.