c# - Automated file upload with additional model -
i have following mvc 4 action
[acceptverbs(httpverbs.post)] public actionresult sendattachment(someviewmodel model, httppostedfilebase attachment) { // implementation goes here }
now want upload file console app controller action using httpwebrequest api can't figure out how set both model , file data in post, matches controller.
any hints on that?
i made changes function (upload files httpwebrequest (multipart/form-data)) problem. worked me. try this:
server side
[httppost] public actionresult sendattachment(someviewmodel model, httppostedfilebase attachment) { return view(); } public class someviewmodel { public int id { get; set; } public string name { get; set; } }
in console app:
static void main(string[] args) { uploaddata( "http://dissertation.lan/home/sendattachment", new namevaluecollection() { {"attachment", @"c:\users\_____\desktop\yourfile.xltx"} }, new namevaluecollection() { {"id", "2"}, {"name","man"} }); } public static void uploaddata(string url, namevaluecollection files, namevaluecollection nvc) { string boundary = "----------------------------" + datetime.now.ticks.tostring("x"); httpwebrequest httpwebrequest2 = (httpwebrequest)webrequest.create(url); httpwebrequest2.contenttype = "multipart/form-data; boundary=" + boundary; httpwebrequest2.method = "post"; httpwebrequest2.keepalive = true; httpwebrequest2.credentials = system.net.credentialcache.defaultcredentials; stream memstream = new system.io.memorystream(); byte[] boundarybytes = system.text.encoding.ascii.getbytes("\r\n--" + boundary + "\r\n"); string formdatatemplate = "\r\n--" + boundary + "\r\ncontent-disposition: form-data; name=\"{0}\";\r\n\r\n{1}"; foreach (string key in nvc.keys) { string formitem = string.format(formdatatemplate, key, nvc[key]); byte[] formitembytes = system.text.encoding.utf8.getbytes(formitem); memstream.write(formitembytes, 0, formitembytes.length); } memstream.write(boundarybytes, 0, boundarybytes.length); string headertemplate = "content-disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n content-type: application/octet-stream\r\n\r\n"; foreach (string key in files.keys) { string header = string.format(headertemplate, key, files[key]); byte[] headerbytes = system.text.encoding.utf8.getbytes(header); memstream.write(headerbytes, 0, headerbytes.length); filestream filestream = new filestream(files[key], filemode.open, fileaccess.read); byte[] buffer = new byte[1024]; int bytesread = 0; while ((bytesread = filestream.read(buffer, 0, buffer.length)) != 0) { memstream.write(buffer, 0, bytesread); } memstream.write(boundarybytes, 0, boundarybytes.length); filestream.close(); } httpwebrequest2.contentlength = memstream.length; stream requeststream = httpwebrequest2.getrequeststream(); memstream.position = 0; byte[] tempbuffer = new byte[memstream.length]; memstream.read(tempbuffer, 0, tempbuffer.length); memstream.close(); requeststream.write(tempbuffer, 0, tempbuffer.length); requeststream.close(); webresponse webresponse2 = httpwebrequest2.getresponse(); stream stream2 = webresponse2.getresponsestream(); streamreader reader2 = new streamreader(stream2); console.write(reader2.readtoend()); webresponse2.close(); httpwebrequest2 = null; webresponse2 = null; }
result
Comments
Post a Comment