.net - c# - ASP.Net Handler Request.Files always empty -
i'm writing c# asp.net application client post files other server. i'm using generic handler handle posted files client server. in handler, context.request.files empty (0 count). believe post method right, because when tried move handler in same domain client, can accept files , save them. problem need save files other server.
here code post files:
private void uploadfilestoremoteurl3(httpfilecollection files) { string url = "http://localhost:19107/catalog/api/dashboard/imagehandler.ashx"; long length = 0; 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"); memstream.write(boundarybytes,0,boundarybytes.length); length += 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 s in files) { httppostedfile file = files[s]; string header = string.format(headertemplate, "file", file.filename); byte[] headerbytes = system.text.encoding.utf8.getbytes(header); memstream.write(headerbytes,0,headerbytes.length); length += headerbytes.length; byte[] buffer = new byte[1024]; int bytesread = 0; while ( (bytesread = file.inputstream.read(buffer, 0, buffer.length)) != 0 ) { memstream.write(buffer, 0, bytesread); length += bytesread; } memstream.write(boundarybytes,0,boundarybytes.length); length += boundarybytes.length; file.inputstream.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); string = reader2.readtoend(); webresponse2.close(); httpwebrequest2 = null; webresponse2 = null; } and here code behind handler receive files:
public void processrequest(httpcontext context) { context.request.contenttype = "multipart/form-data"; int count = context.request.files.count; //always 0 foreach (string s in context.request.files) { string response = ""; httppostedfile file = context.request.files[s]; //code save files } }
public response<list<string>> uplaoadpostimage() { response<list<string>> response = new response<list<string>>(); responseimage objtemp = new responseimage(); list<string> objlist = new list<string>(); try { httpcontextwrapper objwrapper = gethttpcontext(this.request); httpfilecollectionbase collection = objwrapper.request.files; if (collection.count > 0) { foreach (string file in collection) { httppostedfilebase file1 = collection.get(file); stream requeststream = file1.inputstream; image img = system.drawing.image.fromstream(requeststream); //image userimage = objcommon.resizeimage(img, 600, 600); string uniquefilename = file1.filename; img.save(httpcontext.current.request.physicalapplicationpath + "uploadimage\\" + uniquefilename, system.drawing.imaging.imageformat.png); objlist.add(configurationmanager.appsettings["imagepath"] + uniquefilename); requeststream.close(); } response.create(true, 0, messages.formatmessage(messages.uploadimage_sucess, ""), objlist); } else { response.create(false, 0, "file not found.", objlist); } } catch (exception ex) { response.create(false, -1, messages.formatmessage(ex.message), objlist); } return response; } private httpcontextwrapper gethttpcontext(httprequestmessage request = null) { request = request ?? request; if (request.properties.containskey("ms_httpcontext")) { return ((httpcontextwrapper)request.properties["ms_httpcontext"]); } else if (httpcontext.current != null) { return new httpcontextwrapper(httpcontext.current); } else { return null; } }
Comments
Post a Comment