Class HttpFile

Synopsis

#include <lib/inc/drogon/MultiPart.h>

class DROGON_EXPORT HttpFile

Description

This class represents a uploaded file by a HTTP request.

Methods

HttpFile
fileContentreturn the content of the file.
fileDatareturn the pointer of the file data.
fileLengthReturn the file length.
getFileExtensionReturn the file extension; Note: After the HttpFile object is destroyed, do not use this string_view object.
getFileNameReturn the file name;.
getFileTypeReturn the type of file.
getItemNameReturn the name of the item in multiple parts.
getMd5Return the md5 string of the file.
save overloadSave the file to the file system.
save overloadSave the file to.
saveAsSave the file to file system with a new name.
setFileSet the contents of the file, usually called by the MultiPartParser parser.
setFileNameSet the file name, usually called by the MultiPartParser parser.

Source

Lines 33-112 in lib/inc/drogon/MultiPart.h.

class DROGON_EXPORT HttpFile
{
  public:
    HttpFile(std::shared_ptr<HttpFileImpl> &&implPtr);
    /// Return the file name;
    const std::string &getFileName() const;

    /// Return the file extension;
    /// Note: After the HttpFile object is destroyed, do not use this
    /// string_view object.
    string_view getFileExtension() const;

    /// Return the name of the item in multiple parts.
    const std::string &getItemName() const;

    /// Return the type of file.
    FileType getFileType() const;

    /// Set the file name, usually called by the MultiPartParser parser.
    void setFileName(const std::string &fileName);

    /// Set the contents of the file, usually called by the MultiPartParser
    /// parser.
    void setFile(const char *data, size_t length);

    /// Save the file to the file system.
    /**
     * The folder saving the file is app().getUploadPath().
     * The full path is app().getUploadPath()+"/"+this->getFileName()
     */
    int save() const;

    /// Save the file to @param path
    /**
     * @param path if the parameter is prefixed with "/", "./" or "../", or is
     * "." or "..", the full path is path+"/"+this->getFileName(),
     * otherwise the file is saved as
     * app().getUploadPath()+"/"+path+"/"+this->getFileName()
     */
    int save(const std::string &path) const;

    /// Save the file to file system with a new name
    /**
     * @param fileName if the parameter isn't prefixed with "/", "./" or "../",
     * the full path is app().getUploadPath()+"/"+filename, otherwise the file
     * is saved as the filename
     */
    int saveAs(const std::string &fileName) const;

    /**
     * @brief return the content of the file.
     *
     * @return string_view
     */
    string_view fileContent() const
    {
        return string_view{fileData(), fileLength()};
    }

    /// Return the file length.
    size_t fileLength() const noexcept;

    /**
     * @brief return the pointer of the file data.
     *
     * @return const char*
     * @note This function just returns the beginning of the file data in
     * memory. Users mustn't assume that there is an \0 character at the end of
     * the file data even if the type of the file is text. One should get the
     * length of the file by the fileLength() method, or use the fileContent()
     * method.
     */
    const char *fileData() const noexcept;

    /// Return the md5 string of the file
    std::string getMd5() const;

  private:
    std::shared_ptr<HttpFileImpl> implPtr_;
};





Add Discussion as Guest

Log in