A
aq100
Unregistered / Unconfirmed
GUEST, unregistred user!
程序通过客户端向WEB服务器端上传文件
比如把本地的文件dd.doc上传到WEB服务器的根目录temp下,Tinternet采用http协议方式
我在向远端写入文件时总是不成功internetwriterfile的结果为false;
下面是整体流程代码,望大拿指点
const
Buffsize = 1024;
var
HINTERNET : Pointer;
httpContext: Integer;
HttpServer,UserID,Passwd:String;
BufferIn:INTERNET_BUFFERS;
RemoteFile:String;
LocalFile:String;
httpFile:Thandle;
readInFWORD;
oBytesWrittenWORD;
ReadOutWORD;
cRead, rRead:bool;
iStart, iCount, Count: DWORD;
begin
LocalFile:='c:/dd.doc';
RemoteFile:='//temp//dd.doc';
//创建以HTTP的协议连接方式
HttpNet := InternetOpen('127.0.0.1', INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
HttpConnect := InternetConnect(HttpNet, PChar(HttpServer), 0, PChar(UserID),
PChar(Passwd), INTERNET_SERVICE_HTTP, 0, httpContext);
//打开远端目录并设置传输数据
HttpRequest:= HttpOpenRequest (HttpConnect,'PUT',Pchar(RemoteFile),
nil, nil, nil, 0,0); //'GET'
//打开本地文件获得handle
httpFile := CreateFile(Pchar(LocalFile), GENERIC_READ, FILE_SHARE_READ,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
//文件大小
BufferIn.dwBufferTotal:= GetFileSize(httpFile, nil);
//发送请求写入的数据缓存大小
HttpSendRequestEx(HttpRequest, @BufferIn, nil, HSR_INITIATE, 0);
ReadInF:=0;
iCount := 1024;
repeat
//读本地文件
ReadFile(httpFile,pBuffer, iCount, Count,nil);
ReadInF:=ReadInF+Count;
//写到远端服务器
rRead:=InternetWriteFile(httpRequest,@pBuffer,Count,oBytesWritten);
//这里没有成功,疑点一是在连接服务器,设置读写文件等参数出错,疑点二是读本地文件数据写到远端服务器出错,可能还有其他的未知因数,请大拿指点。
if(rRead) then
showmessage('internetWriteFile success!');
until ReadInF=BufferIn.dwBufferTotal;
{
function InternetWriteFile(
hFile: HINTERNET;
lpBuffer: Pointer;
dwNumberOfBytesToWrite: DWORD;
var lpdwNumberOfBytesWritten: DWORD): BOOL; stdcall;
BOOL WriteFile(
HANDLE hFile, //写入文件句柄
LPCVOID lpBuffer, //写入数据指针
DWORD nNumberOfBytesToWrite, //要写入字节数量
LPDWORD lpNumberOfBytesWritten, //已写入字节数地址
LPOVERLAPPED lpOverlapped ); //异步I/O结构指针
}
CloseHandle (httpFile);
HttpEndRequest(HttpRequest, nil, 0, 0);
InternetCloseHandle(HttpRequest);
InternetCloseHandle(HttpConnect);
InternetCloseHandle(HttpNet);
end;
end;
end;
end;
备注,这里引用的是wininet单元,我只针对以http协议中的internetwriterfile方式进行上传,所以看管只需UP,俺就非常感谢了.
如果大家看不明白,我下次给你们VC的源码,我是看着它改的,只是不成功,如果大家需要,给个mail,我给你们发过去。
比如把本地的文件dd.doc上传到WEB服务器的根目录temp下,Tinternet采用http协议方式
我在向远端写入文件时总是不成功internetwriterfile的结果为false;
下面是整体流程代码,望大拿指点
const
Buffsize = 1024;
var
HINTERNET : Pointer;
httpContext: Integer;
HttpServer,UserID,Passwd:String;
BufferIn:INTERNET_BUFFERS;
RemoteFile:String;
LocalFile:String;
httpFile:Thandle;
readInFWORD;
oBytesWrittenWORD;
ReadOutWORD;
cRead, rRead:bool;
iStart, iCount, Count: DWORD;
begin
LocalFile:='c:/dd.doc';
RemoteFile:='//temp//dd.doc';
//创建以HTTP的协议连接方式
HttpNet := InternetOpen('127.0.0.1', INTERNET_OPEN_TYPE_PRECONFIG,nil,nil,0);
HttpConnect := InternetConnect(HttpNet, PChar(HttpServer), 0, PChar(UserID),
PChar(Passwd), INTERNET_SERVICE_HTTP, 0, httpContext);
//打开远端目录并设置传输数据
HttpRequest:= HttpOpenRequest (HttpConnect,'PUT',Pchar(RemoteFile),
nil, nil, nil, 0,0); //'GET'
//打开本地文件获得handle
httpFile := CreateFile(Pchar(LocalFile), GENERIC_READ, FILE_SHARE_READ,
nil, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,0);
//文件大小
BufferIn.dwBufferTotal:= GetFileSize(httpFile, nil);
//发送请求写入的数据缓存大小
HttpSendRequestEx(HttpRequest, @BufferIn, nil, HSR_INITIATE, 0);
ReadInF:=0;
iCount := 1024;
repeat
//读本地文件
ReadFile(httpFile,pBuffer, iCount, Count,nil);
ReadInF:=ReadInF+Count;
//写到远端服务器
rRead:=InternetWriteFile(httpRequest,@pBuffer,Count,oBytesWritten);
//这里没有成功,疑点一是在连接服务器,设置读写文件等参数出错,疑点二是读本地文件数据写到远端服务器出错,可能还有其他的未知因数,请大拿指点。
if(rRead) then
showmessage('internetWriteFile success!');
until ReadInF=BufferIn.dwBufferTotal;
{
function InternetWriteFile(
hFile: HINTERNET;
lpBuffer: Pointer;
dwNumberOfBytesToWrite: DWORD;
var lpdwNumberOfBytesWritten: DWORD): BOOL; stdcall;
BOOL WriteFile(
HANDLE hFile, //写入文件句柄
LPCVOID lpBuffer, //写入数据指针
DWORD nNumberOfBytesToWrite, //要写入字节数量
LPDWORD lpNumberOfBytesWritten, //已写入字节数地址
LPOVERLAPPED lpOverlapped ); //异步I/O结构指针
}
CloseHandle (httpFile);
HttpEndRequest(HttpRequest, nil, 0, 0);
InternetCloseHandle(HttpRequest);
InternetCloseHandle(HttpConnect);
InternetCloseHandle(HttpNet);
end;
end;
end;
end;
备注,这里引用的是wininet单元,我只针对以http协议中的internetwriterfile方式进行上传,所以看管只需UP,俺就非常感谢了.
如果大家看不明白,我下次给你们VC的源码,我是看着它改的,只是不成功,如果大家需要,给个mail,我给你们发过去。