cgicc开发(文件上传)

embedded/2024/9/25 9:39:55/
//cgicc文件上传封装
void UploadSoftware()
{// 初始化CGIC环境Cgicc cgi;// 获取上传的文件file_iterator fileIter = cgi.getFile("button_browse"); //from表单中,输入为文件属性(type=file)的name属性值if (fileIter == cgi.getFiles().end()){
#if (DEBUG == true)cout << "file: " << cgi.getFiles().size() << endl;
#endif}else{FormFile& file = *fileIter;
#if (DEBUG == true)std::cout << "<p>file: " << std::endl;std::cout << "<br>    name: "<< file.getName() << std::endl;std::cout << "<br>    filename: "<< file.getFilename() << std::endl;std::cout << "<br>    type: "<< file.getDataType() << "; cgicc::HTTPHTMLHeader=" << cgicc::HTTPHTMLHeader() << std::endl;std::cout << "<br>    size: "<< file.getDataLength() << std::endl;std::cout << "<br>    content: "<< file.getData() << std::endl;
#endif// 保存文件到服务器指定路径ofstream outfile("/opt/software/" + file.getFilename(), ios::out | ios::binary);outfile << file.getData();outfile.close();}
}
<!--html from表单部分-->
<script>
function SubmitUpgrade(obj)
{if(document.getElementById("button_browse").value == "" && document.getElementById("software_bag_url").value == ""){alert("软件升级包未选中");return false;}var ip_pos = radio_json.base.localHost.split('.');var first_ip_pos = Number(ip_pos[3]) + 100;var second_ip_pos = Number(ip_pos[3]) + 200;var first_ip = ip_pos[0] + "." + ip_pos[1] + "." + ip_pos[2] + "." + first_ip_pos;var second_ip = ip_pos[0] + "." + ip_pos[1] + "." + ip_pos[2] + "." + second_ip_pos;console.log("first_ip=" + first_ip + "; second_ip=" + second_ip + "; third=" + radio_json.base.localHost);obj.action="/cgi-bin/uphold_tools/uphold_tools.cgi?upgrade_software&1&" + first_ip + "&" + second_ip + "&" + radio_json.base.localHost + "&client&1";obj.submit();
}
</script>
<form enctype="multipart/form-data" id="form_upgrade" name="form_upgrade" method="post"><td class="css12r">软件升级包:</td><td><input name="software_bag_url" type="text" id="software_bag_url" size="22" maxlength="22" /></td><td><p class="css12l"><input type="file" name="button_browse" id="button_browse" /></p></td><td><p class="css12l"><input type="submit" name="button_upgrade" id="button_upgrade" value="升级" onclick="return SubmitUpgrade(this.form)"/></p></td>
</form>

测例:

 

 注意:

1)上传不同类型的文件需要设置不同的 HTTP Content Header。在文件上传时,Content Header 包含了关于上传内容的类型和其他重要信息,这对于服务器正确解析和处理上传的文件至关重要。一般,cgi程序在main函数开始的位置需要指明Content Header:

cout << "Content-type:text/html;charset=utf-8\r\n\r\n";

当上传.tar.gz类型的压缩包文件时,Content-type:text/html;已不再满足,需要设置tar.gz的Content Header:

HTTPContentHeader("application/x-tar-gz");

2)上传的文件有大小限制。

        一般在web server的配置文件中有指定单次POST的数据流大小,若不指明,默认1M。例如:boa web server的boa.conf中修改上限为20M:20*1024*1024 = 20M = 20971520

# SinglePostLimit: The maximum allowable number of bytes in 
# a single POST.  Default is normally 1MB.SinglePostLimit 20971520

上传文件超过单次请求数据流上限时,报错: 


http://www.ppmy.cn/embedded/43328.html

相关文章

git显示提交次数

git shortlog 是一个特殊版本的 git log 命令&#xff0c;旨在创建发布公告。它将每个提交按作者分组&#xff0c;并显示每个提交消息的第一行。这是一种快速查看不同作者在项目中的贡献的方式。 以下是 git shortlog 的一些常用参数&#xff1a; -n 或 --numbered&#xff1…

北美码农面试流程,北美码农面试经验

听说有人还不知道OA是什么&#xff1f;HR电联跟Tech phone interview什么区别&#xff1f;Onsite interview有哪些关键点具体考些什么不清楚&#xff1f;下面为大家分享北美码农面试流程。 SDE面试流程主要包含&#xff1a; Online assessment HR contact email/phone Tech…

跳绳技巧一:蝴蝶步

跳绳蝴蝶步 跳绳蝴蝶步是一种跳绳技巧&#xff0c;结合了跳绳和舞蹈动作&#xff0c;形成一种优美而富有节奏感的跳跃方式。以下是学习和掌握跳绳蝴蝶步的基本步骤&#xff1a; 1. 基础准备 确保跳绳长度适合你的身高&#xff0c;站在绳子中间&#xff0c;两端绳柄应该到达你…

Ansible实战YAML语言完成apache的部署,配置,启动全过程

&#x1f3e1;作者主页&#xff1a;点击&#xff01; &#x1f3dd;️Ansible专栏&#xff1a;点击&#xff01; ⏰️创作时间&#xff1a;2024年5月24日15点59分 目录 &#x1f4af;趣站推荐&#x1f4af; &#x1f38a;前言 ✨️YAML语言回顾 &#x1f386;1.编写YAML文…

C++

定义自己的命名空间my_sapce&#xff0c;在my_sapce中定义string类型的变量s1&#xff0c;再定义一个函数完成对字符串的逆置。

MySQL(进阶)--索引

目录 一.存储引擎 1.MySQL体系结构​编辑 2.存储引擎简介 3.存储引擎特点 (1.InnoDB (2.MyISAM (3.Memory 4.存储引擎选择 二.索引 1.索引概述 2.索引结构 3.索引分类 4.索引语法 (1.创建索引 (2.查看索引 (3.删除索引 5.SQL性能分析 (1.SQL执行频率 (2.慢查…

js深入理解对象的 属性(properties)的特殊 特性(attributes)

对象 js对象 // 构造一个对象 let obj {}; let obj new Object(); 我们知道js中一切皆对象&#xff0c;对象是一个键值对集合&#xff08;key: value)&#xff0c;一个键(key)对应一个值(value)&#xff0c;而每个键都是这个对象的属性&#xff0c;我们可以通过对象的属性来…

通过 python 操作mongodb

库引入 Python 要连接 MongoDB 需要 MongoDB 驱动&#xff0c;这里我们使用 PyMongo 驱动来连接。 import pymongo 链接数据库 创建数据库需要使用 MongoClient 对象&#xff0c;并且指定连接的ip和端口号。 myclientpymongo.MongoClient("localhost",27017)#连接…