AZorbas That's me , Alexis Zorbas ! | voilà pour remplir un formulaire et le traiter par mon serveur j'utilise ça et ça marche.
Maintenant tetedeiench me dit que pour un fichier je dois faire pareil. Le prob c'est que je sais pas comment faire pour envoyer un fichier ET remplir les champs comme pour un formulaire multipart/form-data.
Code :
- procedure TForm1.Button1Click(Sender: TObject);
- var
- aStream: TMemoryStream;
- Params: TStringStream;
- begin
- aStream := TMemoryStream.create;
- Params := TStringStream.create('');
- Params.WriteString(URLEncode('comment=' + 'test' + '&'));
- Params.WriteString(URLEncode(nom=' + 'test'));
- IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
- IdHTTP1.Post('http://localhost/page.php', Params, aStream);
- end;
|
Sinon j'avais trouvé cela (http://beta.experts-exchange.com/P [...] 50661.html) :
Code :
- 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;
|
Mais mon formulaire ne semble pas comprendre ce qu'on lui envoit.
Voilà j'espère avoir été assez clair.
Est-il possible d'envoyer un fichier aussi "facilement" que la première méthode ?
Merci d'avance !
 Message édité par AZorbas le 27-07-2003 à 13:55:53 ---------------
www.astrocosmos.net, le portail belge francophone sur l'astronomie et les Sciences.
|