如何实现用wininet上传文件

zjblue 2005-03-21 11:05:58
我现在用CInternetSession,CHttpConnection,CHttpFile这几个类来实现上传文件到服务器
但是我总是不成功
不知道谁能给出相类似的代码我参考参考?
还有就是IIS服务器这边应该如何设置才能上传文件
...全文
843 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
zjblue 2005-03-23
  • 打赏
  • 举报
回复
用http的put协议上传,需要做两个步骤
第一步就是需要在iis管理器里面设置“写”权限,可以是根目录,也可以是虚拟目录
第二步就是需要在放上传文件的那个目录的NTFS权限设置中,添加IU_USER(Internet匿名访问用户)的写权限。

我就是因为第二步没有设置所以搞了差不多一周。不知道在fat32格式下是否需要设置。

总之,在这样的设置下,就可以用我上面的那个类上传文件了
zjblue 2005-03-23
  • 打赏
  • 举报
回复
然后是cpp文件
#include "StdAfx.h"
#include ".\uploadfile.h"

UpLoadFile::UpLoadFile(void)
{
}

UpLoadFile::~UpLoadFile(void)
{
}

BOOL UpLoadFile::UseHttpSendReqEx(CHttpFile* httpFile, DWORD dwPostSize,CString strLocalFile)
{
try
{
DWORD dwRead,dwRet;

BYTE* buffer;
TRACE("Local file:%s\n",strLocalFile);
FILE* fLocal;
if((fLocal=fopen(strLocalFile,"rb"))==NULL)
{
TRACE("Can't open the file:%s,maybe it doesn't exist!\n",strLocalFile);
return false;
}
fseek(fLocal,0L,SEEK_END);
dwRead=ftell(fLocal);
rewind(fLocal);
buffer=(BYTE *)malloc(dwRead);
if(!buffer){
TRACE("not enough memory!\n");
return false;
}

TRACE("length of file:%d\n",dwRead);
dwRead=fread(buffer,1,dwRead,fLocal);
dwPostSize=dwRead;

INTERNET_BUFFERS BufferIn;
DWORD dwBytesWritten;
BOOL bRet;
BufferIn.dwStructSize = sizeof( INTERNET_BUFFERS ); // Must be set or error will occur
BufferIn.Next = NULL;
BufferIn.lpcszHeader = NULL;
BufferIn.dwHeadersLength = 0;
BufferIn.dwHeadersTotal = 0;
BufferIn.lpvBuffer = NULL;
BufferIn.dwBufferLength = 0;
BufferIn.dwBufferTotal = dwPostSize; // This is the only member used other than dwStructSize
BufferIn.dwOffsetLow = 0;
BufferIn.dwOffsetHigh = 0;

httpFile->SendRequestEx(&BufferIn,NULL,HSR_INITIATE,0);
//httpFile->SendRequestEx(dwPostSize);

httpFile->Write( buffer, dwPostSize);

if(!httpFile->EndRequest(0,0,0))
{
TRACE( "Error on HttpEndRequest %lu \n", GetLastError());
return FALSE;
}

fclose(fLocal);
free(buffer);
return TRUE;
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;
}


BOOL UpLoadFile::Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile)
{
try
{
DWORD dwPostSize=0;
INTERNET_PORT intServerPort=atoi(strServerPort);

CInternetSession internetSession("my session");


CHttpConnection* httpConnection = internetSession.GetHttpConnection(strServerIP,intServerPort);
if(httpConnection == NULL)
{
TRACE( "Failed to connect\n" );
return FALSE;
}

CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile,NULL,0,NULL,NULL,INTERNET_FLAG_DONT_CACHE);
//CHttpFile* httpFile = httpConnection->OpenRequest(CHttpConnection::HTTP_VERB_PUT,strRemoteFile);
if(httpFile == NULL)
{
TRACE( "Failed to open request handle\n" );
return FALSE;
}

if(UseHttpSendReqEx(httpFile, dwPostSize,strLocalFile))
{

TRACE( "\nSend Finished.\n" );

httpFile->Close();
httpConnection->Close();
internetSession.Close();
return TRUE;
}

httpFile->Close();
httpConnection->Close();
internetSession.Close();
}
catch (CInternetException* pEx)
{
//catch errors from WinInet
}
return FALSE;

}


zjblue 2005-03-23
  • 打赏
  • 举报
回复
问题已经解决

下面是我写的一个类,用http put协议上传文件的

首先是头文件
#pragma once
#include <afxinet.h>

class UpLoadFile
{
public:
UpLoadFile(void);
~UpLoadFile(void);

BOOL Upload(CString strLocalFile,CString strServerIP,CString strServerPort,CString strRemoteFile);
private:
BOOL UseHttpSendReqEx(CHttpFile* httpFile,DWORD dwPostSize,CString strLocalFile);
};
老夏Max 2005-03-22
  • 打赏
  • 举报
回复
FTP例子
FTP Wanderer - FTP Client using WININET
http://www.vccode.com/file_show.php?id=665
老夏Max 2005-03-22
  • 打赏
  • 举报
回复
http://www.vccode.com/file_show.php?id=503
zjblue 2005-03-22
  • 打赏
  • 举报
回复
我试了不行,是否在IIS需要设置?
我怀疑是我的IIS不支持http put功能
zjblue 2005-03-21
  • 打赏
  • 举报
回复
有没有用PUT来实现的
jun_01 2005-03-21
  • 打赏
  • 举报
回复
帮你顶,
你也帮我顶一下帖子吧:
http://community.csdn.net/Expert/TopicView3.asp?id=3865781
Kudeet 2005-03-21
  • 打赏
  • 举报
回复
http://search.csdn.net/Expert/topic/953/953720.xml?temp=.1335871
http://search.csdn.net/Expert/topic/717/717685.xml?temp=.7839929
http://search.csdn.net/Expert/topic/953/953720.xml?temp=.1335871
sharkhuang 2005-03-21
  • 打赏
  • 举报
回复
msdn就有例子
zjblue 2005-03-21
  • 打赏
  • 举报
回复
up
kingzai 2005-03-21
  • 打赏
  • 举报
回复
HTTP POST-MultiPartFormData
http://www.codeproject.com/internet/simplehttpclient.asp

18,356

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC 网络编程
c++c语言开发语言 技术论坛(原bbs)
社区管理员
  • 网络编程
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧