c# - Is it possible to POST a file and some data in the same call to a webservice? -
i webservice able take username , binary file parameter in same call it. possible?
i thought use webclient.credentials , set username way, doesnt seem working
the simplest way turn binary data base64 string. string can transmitted passing parameter or inside xml string.
web service method:
now depending on size files, incur performance loss, not substantial one.
to convert byte array base64 string:
byte[] data = getsomedata(...);//whatever use byte array. string sdata = system.convert.tobase64string(data );
i choose method on webclient.fileupload() because have more control , more options. rather post page, can send additional information file, usernames, passwords etc. if put them neat xml file.
[webmethod] public bool submitfile(string username, string file) { }
or
[webmethod] public bool submitfile(string data) { }
where data be:
<data> <authentication> <username></username> <password></password> </authentication> <files> <file> <!-- base64 string goes here --> </file> </files> </data>
you can choose keep file byte array, , pass that, i'm fan of using xml in webservices, including file inside xml snippet (easier log , record soap information).
[webmethod] public void putfile(byte[] buffer, string filename, string username) { binarywriter binwriter = new binarywriter(file.open(server.mappath(filename), filemode.createnew, fileaccess.readwrite)); binwriter.write(buffer); binwriter.close(); }
webclient.fileupload method.
you want use cookies here. need somehow authenticate server (if go route) or @ least have username cookie server understand.
when send file, make sure have appropriate cookie going along file server knows do.
an example : http://www.codeproject.com/kb/cs/uploadfileex.aspx
Comments
Post a Comment