Пример #1
0
// 输出http协议头部
int Page_Image::OutHead()
{
    FUNCTION_TRACK(); // 函数轨迹跟综

    Connect * const connect = m_request->GetConnect();
    string filename = m_request->GetField("file");
    string fullpath = "";

    if("" == filename)
    {
        /*
         * 是直接请求图片,如:
         * http://192.168.1.100:17890/logo.gif
         */
        filename = m_request->GetPageName();
        fullpath = GlobalConfig::instance()->HtmlDir() + filename;
    }
    else
    {
        /*
         * 打开用户图片,如:
         *  http://192.168.1.100:17890/image?file=logo.gif
         */
        const string &username = m_request->GetCurrentUser();
        const string &key = GetCurrentKey();
        User *user = User::Get( username );
        fullpath = user->AttachDir() + key + "." + filename; // 图片存于附件目录中
    }

    LOG_DEBUG("file=[%s]", fullpath.c_str());


    if( "" == filename || !m_file.Open(fullpath) )
    {
        Page::OutHead();

        const string str = "没有图片: <font color='red'>" + filename + "</font>";
        LOG_ERROR("Can't open file: [%s]", fullpath.c_str());
        // 发送到浏览器
        connect->Send(str);
        return ERR;
    }

    const string &size = IntToString(m_file.Size());
    // 图像显示
    const string html = ""
                        "HTTP/1.1 200 OK\n"
                        "Accept-Ranges: bytes\n"
                        "Content-Length: " + size + "\n"
                        "Connection: Keep-Alive\n"
                        "Content-Type: image/png jpg jpeg gif bmp\n"
                        "\n";

    // 发送
    return connect->Send(html) == html.length() ? OK : ERR;
}
Пример #2
0
// 输出http协议头部
int Page_Download::OutHead()
{
    FUNCTION_TRACK(); // 函数轨迹跟综

    Connect * const connect = m_request->GetConnect();
    string filename = m_request->GetField("file");
    string fullpath = "";


    /*
     * 打开用户文件,如:
     *  http://192.168.1.100:17890/download?file=logo.gif
     */
    const string &username = m_request->GetCurrentUser();
    const string &key = GetCurrentKey();
    User *user = User::Get( username );
    fullpath = user->AttachDir() + key + "." + filename;

    LOG_DEBUG("file=[%s]", fullpath.c_str());

    if( "" == filename || !m_file.Open(fullpath) )
    {
        Page::OutHead();

        const string str = HtmlAlert("没有文件: " + filename + ",可能文件已被删除。");
        LOG_ERROR("Can't open file: [%s]", fullpath.c_str());
        // 发送到浏览器
        connect->Send(str);
        return ERR;
    }

    const string &size = IntToString(m_file.Size());

    // 文件下载头部格式
    const string html = ""
                        "HTTP/1.1 200 OK\n"
                        "Accept-Ranges: bytes\n"
                        "Content-Disposition: attachment; filename=\"" + FilenameDecode(filename) + "\"\n"
                        "Content-length: " + size + "\n"
                        "Connection: Keep-Alive\n"
                        "Content-Type: application/ms-excel\n"
                        "\n";

    // 发送
    return connect->Send(html) == html.length() ? OK : ERR;
}