因服务器端只开放了80端口,不提供FTP等服务,如何才能利用HTTP协议上传文件呢?首先声明在服务器端已经拥有写文件的权限,并且也准备好了处理文件上传的脚本,可是现在不知如何才能根据网页Form上传文件的方式用Delphi编程来实现呢?看到WinHTTP好像可以用Stream的方式来上传文件,但是没有源代码,不知道如何实现。还请各位大侠多多指教,必有重谢!另外准备了200分的小礼,不成敬意!

解决方案 »

  1.   

    http://www.365base.com/Soft/sort8/151/2005/2005080115619.html
    http://www.study888.com/computer/pro/Delphi/code/200506/91544.html你看看这个对你有帮助没
      

  2.   

    感谢xixuemao,还希望有别的大侠多多指点……
      

  3.   

    以前写过,现在还在用,服务器端是.Net的接收程序,我找一下源码。
      

  4.   

    //indy一定要用9.0.18版本,否则上传不成功。uses
      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;
      

  5.   

    下面是服务器端insertImage.cs的代码:private void Page_Load(object sender, System.EventArgs e)
    {
    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;
    }
      

  6.   

    program HttpPut; {$APPTYPE CONSOLE} uses 
    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. 
      

  7.   

    感谢Dlwxn(蓝天) 、constantine(飘遥的安吉儿)!
    请到http://community.csdn.net/Expert/topic/4258/4258643.xml?temp=.6667902得到承诺的分数!