delphi - posting a file as part of a form -
i trying post file , other data website in app writing using delphi xe8 not working. when monitor network traffic using "microsoft network monitor 3.4", file partially sent , none of other data sent. have tried both indy 10 , new tnethttpclient , got same result tells me doing 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); // returns 'image/pjpeg' instead of 'image/jpeg'. have manually fixed , did not change 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); // 500 error server bad data . . . poststream.free(); responsestream.free(); mcookies.free(); phttp.free(); end;
by way getmimetypefromfile returns wrong value if hardcode correct one, not make 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 server working correctly because application (written in intel xda) works fine. image valid, , get calls make before works well. need help. thank in advance
when monitor network traffic using "microsoft network monitor 3.4", file partially sent , none of other data sent.
are sure monitor not displaying partial data in ui? packet sniffer wireshark more reliable (or attach tidlog...
component tidhttp
), show being transmitted. possible way posting tidmultipartformdatastream
send portion of file , skip other fields altogether if socket disconnected while transmitting file.
mimestr := getmimetypefromfile(fieldvalue); // returns 'image/pjpeg' instead of 'image/jpeg'.
internally, getmimetypefromfile()
builds own list of hard-coded mime types , uses os information overwrite list. default value hard-coded .jpg
image/jpeg
. when queries os, sees image/jpeg
, image/pjpeg
(in order) registered .jpg
, image/pjpeg
last mime type seen .jpg
, getmimetypefromfile()
ends returning.
phttp.request.contenttype := poststream.requestcontenttype;
you not need that, post() handles internally you.
phttp.request.acceptencoding := 'gzip, deflate';
do not @ all. post()
handles internally you, based on whether tidhttp.compressor
assigned , ready.
Comments
Post a Comment