分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 网页技术

Libevent::evhttp服务器下载

发布时间:2023-09-06 02:29责任编辑:赖小花关键词:http
void http_handler_Get_Download(struct evhttp_request *req, void *arg){ ???if (req == NULL) ???{ ???????return; ???} ???const char *uri = evhttp_request_get_uri(req); ???string strUrl(uri); ???string strFilePath = DPC::get_Url_path(strUrl, "path="); ???printf("FilePath = %s \n", strFilePath.c_str()); ???string strFileName = DPC::get_FileName_path(strUrl, "/"); ???strFileName = "attachment; filename=" + strFileName; ???//查看文件是否存在 ???if (access(strFilePath.c_str(), 0) == -1) ???{ ???????evhttp_send_error(req, HTTP_BADREQUEST, 0); ???????return; ???} ???//========= ???std::ifstream t; ???t.open(strFilePath.c_str(), ios::in | ios::binary); ???t.seekg(0, std::ios::end); ???long length = t.tellg(); ???t.seekg(0, std::ios::beg); ???char *buffer = new char[length]; ???t.read(buffer, length); ???t.close(); ???char FileSize[10] = { ‘\0‘ }; ???snprintf(FileSize, sizeof(FileSize), "%d", length); ???time_t timep; ???time(&timep); ???char s[50]; ???sprintf(s, ctime(&timep)); ???std::string strDate = std::string(s, s + (strlen(s) - 1)); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Server", "Unix"); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Type", "application/octet-stream; charset=utf-8"); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Disposition", strFileName.c_str()); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Content-Length", FileSize); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Date", strDate.c_str()); ???evhttp_add_header(evhttp_request_get_output_headers(req), "Connection", "close"); ???//========= ???//回响应 ???struct evbuffer *retbuff = NULL; ???retbuff = evbuffer_new(); ???if (retbuff == NULL) ???{ ???????return; ???} ???evbuffer_add(retbuff, buffer, length); ???//evbuffer_add_printf(retbuff, "123"); ???evhttp_send_reply(req, HTTP_OK, "Client", retbuff); ???evbuffer_free(retbuff);}

Libevent::evhttp服务器下载

原文地址:https://www.cnblogs.com/osbreak/p/10247991.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved