用xmlhttp读取字符会了但用来下文件不小弟现在想用来下载一个图片,请教在delphi中怎么保存啊? ResponseXML、ResponseText、ResponseStream、ResponseBody

解决方案 »

  1.   

    Call below method SaveFile to save the file to location you want.Example:savefile('http://sp2.yokacdn.com/photos/2f/79/769007/photo_1090174_500.jpg',
                  'd:\flower.jpg');procedure SaveFile(url, filename : String);
    var
      xmlhttp : IXMLHTTPRequest;
      fstream : TFileStream;
      stream  : TOleStream;
    begin
      xmlhttp := CoXMLHTTP.Create;
      try
         xmlhttp.Open('GET',url,false,EmptyParam, EmptyParam);
         xmlhttp.Send(EmptyParam);
         stream := TOleStream.Create(IUnknown(xmlhttp.ResponseStream) as IStream);
         fstream := TFileStream.Create(filename,fmCreate);
         stream.Position:=0;
         fstream.CopyFrom(stream,stream.Size);
      finally
             if Assigned(fstream) then
                fstream.Free;
             if Assigned(stream) then
                stream.Free;
             xmlhttp := nil;
      end;
    end;
    Hope it helps.//Ali
      

  2.   

    Don't forget to add below units to unit uses clause
    uses AxCtrls,MSXML2_TLB,ActiveX;
    Forgot to mention in above post :)//Ali