|
VC上传文件post到web服务器(VC上传文件,图片或视频)
编译开发环境:WIN10+64位,VC++6.0
功能:VC上传文件post到web服务器(VC上传文件,图片或视频)。代码为本站原创,转移请注意出处。
1.项目功能需求,需要VC++上传文件或图片到WEB服务器,搜索了一些算法,现在做成一个示例项目,有兴趣的可以下载参考一下。主要代码如下,已经进行了一些整理与说明: - //可以上传任意文件
- void CMyTestDlg::OnButton2()
- {
- // TODO: Add your control notification handler code here
- //调用方式 D:\\05.png 或 D:\\test.rar
- HttpPostFile("", L"D:\\05.png"/*L"E:\\123\\test.txt"*/, "uploadeFile", "application/octet-stream" /*L"text/plain"*/);
- }
- void CMyTestDlg::HttpPostFile(CString url, CString file, CString paramName, CString contentType)
- {
- CInternetSession pSession(_T("ic_PostWav")); //可以随意
- CHttpConnection* pConnect;
- CHttpFile * pFile;
- //通过 url解析出来
- CString pServeIP = _T("192.168.2.58");
- INTERNET_PORT wPort = 7078;
- CString pObject = _T("/UploadServletWeb");
- pConnect = pSession.GetHttpConnection(pServeIP, wPort);
- pFile = pConnect->OpenRequest(CHttpConnection::HTTP_VERB_POST, pObject, NULL, 0, NULL, NULL, INTERNET_FLAG_DONT_CACHE);
- CString boundary = "----1a2b3c4d5e6f";
- //Http 头部
- CString pPostHeader;
- pPostHeader = "Accept:audio/x-wav,text/html,application/xhtml+xml,application/xml,*/*;q=0.9,*/*;q=0.8\r\n";
- pPostHeader += "Content-Type: multipart/form-data;";
- pPostHeader += "boundary=" + boundary + "\r\n";
- pPostHeader += "Connection: keep-alive\r\n";
- CString httpHead;
- httpHead.Format(_T("%s"), pPostHeader);
- pFile->AddRequestHeaders(httpHead);
- //数据帧头
- CString dataTop, name,filename;
- name = "name";
- //test.rar或05.png
- filename = "05.png";//重要:此处名称与上传文件名称相同
- dataTop = "--"+ boundary +"\r\n";
- dataTop += "Content-Disposition:form-data;";
- dataTop += "name="" + name + "";";
- dataTop += "filename="" + filename + ""\r\n";
- dataTop += "Content-Type:" + contentType + "\r\n\r\n";
- byte* pPostTopbytes = (byte*)dataTop.GetBuffer(0);
- //数据包尾
- CString dataEnd;
- dataEnd = "\r\n--" + boundary + "--\r\n";
-
- byte* enderbyte = (byte*)dataEnd.GetBuffer(0);
- CFile cfile;
- cfile.Open(file, CFile::modeRead | CFile::shareDenyRead, NULL);
- DWORD dwSize = dataTop.GetLength() + dataEnd.GetLength() + cfile.GetLength();
- pFile->SendRequestEx(dwSize);
- //写数据头
- pFile->Write(pPostTopbytes, dataTop.GetLength());
- //写数据主体
- int bufflength = 4 * 1024;
- byte* buffer = new byte[bufflength];
- int byteRead = 0;
- while ((byteRead = cfile.Read(buffer, bufflength)) != 0)
- {
- pFile->Write(buffer, byteRead);
- }
- cfile.Close();
- //写数据尾部
- pFile->Write(enderbyte, dataEnd.GetLength());
- //发送文件
- pFile->EndRequest();
- //接收返回
- CString strSentence = _T(""), strGetSentence = _T("");
- DWORD dwRet;
- pFile->QueryInfoStatusCode(dwRet);
- if (HTTP_STATUS_OK == dwRet)
- {
- while (pFile->ReadString(strSentence)) // 读取提交数据后的返回结果
- {
- strGetSentence = strGetSentence + strSentence;
- }
- //todo: 将返回的编码数据转为自己需要的编码数据
- //弹出返回值
- AfxMessageBox(strGetSentence);
- }
- pFile->Close();
- pConnect->Close();
- }
复制代码2.示例项目编译成功如下:
3.运行界面如下:
4.示例程序只能手动在代码中更改上传图片或文件,此处大家可以下载源码后进行优化,此处不再做此处理。下面图片为上传图片后的,并返回服务器图片路径:
5.上传文件后的截图如下(此处需要注意的是代码中的这个filename参数,需要对应上传文件后辍:
6.代码及软件操作图:
代码为本站原创,转移请注意出处。
附件下载:
|
|