I am trying to post a file and some other data to a website in an App that I am writing using Delphi XE8 but it is not working. When I monitor the network traffic using "Microsoft Network Monitor 3.4", the file is only partially sent and none of the other data is sent. I have tried both Indy 10 and the new TNetHTTPClient and got the same result which tells me I am doing something wrong. please help.
Indy Way
procedure TMyClass.SendFile(aUrl: String);
var
mCookies: TIdCookieManager;
pHttp: TIdHTTP;
PostStream: TIdMultiPartFormDataStream;
ResponseStream: TStringStream;
fName, mimeStr: String;
begin
fName := 'Image000.jpg';
mCookies := TIdCookieManager.Create(nil);
pHttp := TIdHTTP.Create(nil);
pHttp.CookieManager := mCookies;
PostStream:= TIdMultiPartFormDataStream.Create();
ResponseStream := TStringStream.Create('');
mimeStr := GetMIMETypeFromFile(fieldValue); // This returns 'image/pjpeg' instead of 'image/jpeg'. I have manually fixed it and it did not change the result
PostStream.AddFile('sourceFile', fName, mimeStr);
PostStream.AddFormField('name1', 'value1');
PostStream.AddFormField('name2', 'value2');
PostStream.AddFormField('name3', 'value3');
PostStream.AddFormField('name4', 'value4');
PostStream.AddFormField('name5', 'value5');
.
.
.
pHttp.Request.ContentType := PostStream.RequestContentType;
pHttp.Request.Accept := '*/*';
pHttp.Request.AcceptLanguage := 'en-us,en';
pHttp.Request.AcceptEncoding := 'gzip, deflate';
pHttp.Post(aUrl, PostStream, ResponseStream); // Get a 500 error from server for bad data
.
.
.
PostStream.Free();
ResponseStream.Free();
mCookies.Free();
pHttp.Free();
end;
by the way GetMIMETypeFromFile returns the wrong value but even if I hardcode the correct one, it does not make any different.
the new XE8 way
procedure TMyClass.SendFile(aUrl: String);
var
mCookies: TCookieManager;
pHttp: TNetHTTPClient;
fName: String;
mpFormData: TMultipartFormData;
respData: IHTTPResponse;
begin
fName := 'Image000.jpg';
mCookies := TCookieManager.Create();
pHttp := TNetHTTPClient.Create(nil);
pHttp.CookieManager := mCookies;
mpFormData := TMultipartFormData.Create();
mpFormData.AddFile('sourceFile', fName);
mpFormData.AddField('name1', 'value1');
mpFormData.AddField('name2', 'value2');
mpFormData.AddField('name3', 'value3');
mpFormData.AddField('name4', 'value4');
mpFormData.AddField('name5', 'value5');
.
.
.
pHttp.ContentType := 'multipart/form-data';
pHttp.Accept := '*/*';
pHttp.AcceptLanguage := 'en-us,en';
pHttp.AcceptEncoding := 'gzip, deflate';
mpFormData.Stream.Position := 0;
respData := pHttp.Post(aUrl, mpFormData.Stream); //Same problem, error 500 here
.
.
.
mpFormData.Free();
pHttp.Free();
mCookies.Free();
end;
I know the server is working correctly because another application (written in Intel XDA) works just fine. the image is valid, and all the Get calls that I make before this works as well. I really need help. Thank you in advance