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 \"Micros
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.
Are you sure the monitor is simply not displaying partial data in its UI? A packet sniffer like Wireshark would be more reliable (or attach a TIdLog...
component to TIdHTTP
), as it will show you everything that is actually being transmitted. The only possible way that posting a TIdMultipartFormDataStream
would only send a portion of the file and skip the other fields altogether is if the socket is disconnected while transmitting the file.
mimeStr := GetMIMETypeFromFile(fieldValue); // This returns 'image/pjpeg' instead of 'image/jpeg'.
Internally, GetMIMETypeFromFile()
builds its own list of hard-coded MIME types and then uses OS information to overwrite that list. The default value that is hard-coded for .jpg
is image/jpeg
. When it then queries the OS, it sees image/jpeg
and image/pjpeg
(in that order) are registered for .jpg
, so image/pjpeg
was the last MIME type seen for .jpg
and that is what GetMIMETypeFromFile()
ends up returning.
pHttp.Request.ContentType := PostStream.RequestContentType;
You do not need that, Post() handles that internally for you.
pHttp.Request.AcceptEncoding := 'gzip, deflate';
Do not do that at all. Post()
handles that internally for you, based on whether TIdHTTP.Compressor
is assigned and ready.