因服务器端只开放了80端口,不提供FTP等服务,如何才能利用HTTP协议上传文件呢?首先声明在服务器端已经拥有写文件的权限,并且也准备好了处理文件上传的脚本,可是现在不知如何才能根据网页Form上传文件的方式用Delphi编程来实现呢?看到WinHTTP好像可以用Stream的方式来上传文件,但是没有源代码,不知道如何实现。还请各位大侠多多指教,必有重谢!另外准备了200分的小礼,不成敬意!
解决方案 »
- 华为,又一个员工自我终结了生命。。。。。。。。
- 关于数据更新的问题,请大侠指导!
- 简单问题:将一个结构体转换为string类型
- 出现这么个怪问题!明白的帮看看!!!
- 有哪位在虾知 hdc 。在线等!
- 简单问题,目录操作问题,请进!在线!
- 对象的创建和释放!!,为什么我在释放我的对象时总是显示内存错误??在线等。。内有代码!!急、急、急!!
- ******一个很简单的问题,用ENTER代替TAB键
- C++builder的Object Inspector是怎么做的?
- 在使用webbrowser控件时,如何不让它弹出script窗口?
- finger finger finger ! ? ! ... $ $
- 程序访问access中的多个数据表
http://www.study888.com/computer/pro/Delphi/code/200506/91544.html你看看这个对你有帮助没
IdHTTP,IdMultipartFormData, IdGlobal;//上传jpeg,返回值是服务器端保存的文件名称。
function UpLoagFile(FileName: String;http: TIdhttp): string;
var
obj : TIdMultiPartFormDataStream;
Url: String;
begin
obj := TIdMultiPartFormDataStream.Create;
try
obj.AddFile('Image',FileName, GetMIMETypeFromFile(FileName));
http.Request.ContentType := obj.RequestContentType;
obj.Position := 0;
Url := 'http://192.168.1.49/insertImage.aspx'; //这个页面负责接收文件
try
Result := http.Post(Url, obj);
except
on E: Exception do
begin
Application.MessageBox(PChar('上传文件失败,错误原因:' + E.Message), ('错误'), MB_OK + MB_ICONERROR);
Result := '';
end;
end;
finally
obj.Free;
end;
end;
{
string filePath = System.Configuration.ConfigurationSettings.AppSettings["UpLoadPath"]+"/";
string fileName; //接受保存上传文件
foreach(string f in Request.Files.AllKeys)
{
string fullName;
HttpPostedFile postFile = Request.Files[f];
if (postFile.ContentLength > 10)
{
fileName = postFile.FileName;
GetFileName( fileName, out fullName );
fullName = filePath + fullName;
postFile.SaveAs ( Server.MapPath ( fullName ) );
Response.Write (fullName.Trim());//客户端的返回值。
}
}
}
//根据时间和上传文件名生成新的文件名
void GetFileName( string fileName, out string fullName )
{
int lastDot = fileName.LastIndexOf( "." );
string fileExtension = fileName.Substring( lastDot, fileName.Length-lastDot );
DateTime current = DateTime.Now;
int year, month, day, hour, minute, second, millsecond;
year = current.Year;
month = current.Month;
day = current.Day;
hour = current.Hour;
minute = current.Millisecond;
second = current.Second;
millsecond = current.Millisecond;
fullName = year.ToString() + month.ToString() + day.ToString() + hour.ToString() + minute.ToString() + second.ToString() + millsecond.ToString() + fileExtension;
}
SysUtils,
HttpPutUnit in 'HttpPutUnit.pas'; Var
Url,FileName : String; Procedure ShowHelp;
Begin
WriteLn('=========== Http Put File Power by 臭要饭的! ============');
WriteLn('');
WriteLn('Useag:'+ParamStr(0)+' <URL> <PutFilePath>');
WriteLn('');
WriteLn('Sample:'+ParamStr(0)+' http://hostname/images/ c:\p.txt'); End; Var
ResultV : Integer;
begin
IF ParamCount <> 2 Then ShowHelp
Else Begin
Url := ParamStr(1);
FileName := ParamStr(2);
WriteLn('');
WriteLn('[*] Now Put File, Please Wait ...');
ResultV := HttpPutFile(Url,FileName);
IF ResultV = 0 then
WriteLn('[-] Connection Faile...')
Else Begin
IF ResultV <> 201 then
WriteLn('[-] Put File Failed,Maybe IIS Can Not write files or Url Has already this file,or other.')
Else
WriteLn('[*] Success...');
WriteLn('[*] Return HTTP Status Code :'+IntTostr(ResultV));
End;
End; { TODO -oUser -cConsole Main : Insert code here }
end. unit HttpPutUnit; interface uses
Windows, Messages, SysUtils, Classes, WinInet; Function HttpPutFile(Url,PutFileName:String) : Integer; implementation Function HttpPutFile(Url,PutFileName:String) : Integer;
var
hSession, hConnect, hRequest: hInternet;
HostName: String;
HostNameExt : String;
InternetFlag: DWord;
AcceptType: LPStr;
Buf : Pchar;
ISize: Integer; procedure CloseHandles;
begin
InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hSession);
end; procedure ParseURL;
var
i: Integer;
begin
if Pos('http://', LowerCase(URL)) <> 0 then
System.Delete(URL, 1, 7); i := Pos('/', URL);
if i=0 then i := length(Url);
HostName := Copy(URL, 1, i);
HostNameExt := Copy(URL, i, Length(URL) - i + 1);
if (Length(HostName) > 0) and (HostName[Length(HostName)] = '/') then
SetLength(HostName, Length(HostName) - 1); IF HostNameExt[length(HostNameExt)] <> '/' then
HostNameExt := HostNameExt + '/';
end; procedure GetHttpStatus; // StatusCode
var
Len, Reserved: DWORD;
begin
Reserved := 0;
Len := SizeOf(Result);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE or HTTP_QUERY_FLAG_NUMBER,
@Result, Len, Reserved);
end; Procedure GetFileToBuf;
var
PFHandle: HWND;
Begin
PFHandle:= FileOpen(PutFileName, fmOpenRead);
Try
IF PFHandle > 0 then Begin ISize:= FileSeek(PFHandle, 0, 2);
GetMem(Buf, ISize);
FileSeek(PFHandle, 0, 0);
FileRead(PFHandle, buf^, ISize);
End;
Finally
FileClose(PFHandle);
End;
End; begin Result := 0;
try
ParseURL;
hSession := InternetOpen(nil,INTERNET_OPEN_TYPE_PRECONFIG, nil, nil, 0);
hConnect := InternetConnect(hSession, PChar(HostName),
INTERNET_DEFAULT_HTTP_PORT,
nil, nil, INTERNET_SERVICE_HTTP, 0, 0); InternetFlag := INTERNET_FLAG_RELOAD; AcceptType := PChar('Accept: ' + '*/*');
hRequest := HttpOpenRequest(hConnect, 'PUT', Pchar(HostNameExt+ExtractFileName(PutFileName)), 'HTTP/1.0',
nil, @AcceptType, InternetFlag, 0); GetFileToBuf;
HttpSendRequest (hRequest, Nil, 0, buf, iSize);
FreeMem(buf); GetHttpStatus; Finally
CloseHandles;
End; end; end.
请到http://community.csdn.net/Expert/topic/4258/4258643.xml?temp=.6667902得到承诺的分数!