《form action=/script/uploadfile.asp ENCTYPE="multipart/form-data"
METHOD=POST>
<input type=file name="filename">
</form>
procedure TForm1.Button1Click(Sender: TObject);
var
Request : TMemoryStream;
FileData : TMemoryStream;
Response : TStringStream;
tempArray : array[0..10000] of char;
tempString : String;
strFileSize : String;
InFile : String;
const
NewLine = chr(13) + chr(10);
begin
Screen.Cursor := crHourGlass;
InFile := 'c:/test.zip';
// Set the properties for HTTP
HTTP.ProtocolVersion := pv1_1;
HTTP.Request.Username := '';
HTTP.Request.Password := '';
HTTP.Request.ProxyServer := '';
HTTP.Request.ProxyPort := 80;
http.Request.ContentEncoding := 'multipart/form-data';
try
// create streams
Response := TStringStream.Create('');
Request := TMemoryStream.Create;
FileData := TMemoryStream.Create;
// load file data
FileData.LoadFromFile( InFile );
FileData.Seek( 0, soFromBeginning );
// copy form data
TempString := '';
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data; name="UserName"' + NewLine;
TempString := TempString + '' + NewLine;
TempString := TempString + edUserName.text + NewLine;
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data; name="PassWord"' + NewLine;
TempString := TempString + '' + NewLine;
TempString := TempString + edPassWord.text + NewLine;
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data; name="FileSize"' + NewLine;
TempString := TempString + '' + NewLine;
TempString := TempString + '1234567' + NewLine;
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data; name="DataFileName"; filename="' + InFile + '"' + NewLine;
TempString := TempString + 'Content-Type: application/octet-stream' + NewLine;
TempString := TempString + '' + NewLine;
// Add data to post
FillChar( temparray, SizeOf( temparray ), #0 );
strpcopy( temparray, tempstring );
Request.Write( temparray, length(tempstring) );
Request.Seek( 0, soFromEnd );
Request.CopyFrom( FileData, FileData.Size );
// copy form data
TempString := '';
TempString := TempString + '' + NewLine;
TempString := TempString + '-----------------------------7d18914904ee' + NewLine;
TempString := TempString + 'Content-Disposition: form-data; name="Submit"' + NewLine;
TempString := TempString + '' + NewLine;
TempString := TempString + 'Submit' + NewLine;
TempString := TempString + '-----------------------------7d18914904ee--' + NewLine;
// Add data to post
FillChar( temparray, SizeOf( temparray ), #0 );
strpcopy( temparray, tempstring );
Request.Write( temparray, length(tempstring) );
// post file
try
HTTP.Post( edUrl.Text, Request, Response);
except
showmessage( "Failed!")
end;
// write out debug text
memo1.lines.Add('===[TForm1.Post - Response.DataString]=================');
memo1.Lines.add( Response.DataString );
finally
Response.Free;
Request.Free;
FileData.Free;
Screen.Cursor := crDefault;
end;
end;