c# - How to Upload file to server using FTP -
c# - How to Upload file to server using FTP -
i have created new window form application in c#. want upload big file.
and want clarify how upload big file (e.g. 100 mb image)
the next code illustration demonstrates using asynchronous operations upload file ftp server.
using system; using system.net; using system.threading; using system.io; namespace examples.system.net { public class ftpstate { private manualresetevent wait; private ftpwebrequest request; private string filename; private exception operationexception = null; string status; public ftpstate() { wait = new manualresetevent(false); } public manualresetevent operationcomplete { {return wait;} } public ftpwebrequest request { {return request;} set {request = value;} } public string filename { {return filename;} set {filename = value;} } public exception operationexception { {return operationexception;} set {operationexception = value;} } public string statusdescription { {return status;} set {status = value;} } } public class asynchronousftpuploader { // command line arguments 2 strings: // 1. url name of file beingness uploaded server. // 2. name of file on local machine. // public static void main(string[] args) { // create uri instance specified uri string. // if uri not correctly formed, uri constructor // throw exception. manualresetevent waitobject; uri target = new uri (args[0]); string filename = args[1]; ftpstate state = new ftpstate(); ftpwebrequest request = (ftpwebrequest)webrequest.create(target); request.method = webrequestmethods.ftp.uploadfile; // illustration uses anonymous logon. // request anonymous default; credential not have specified. // illustration specifies credential // command how actions logged on server. request.credentials = new networkcredential ("anonymous","janedoe@contoso.com"); // store request in object pass // asynchronous operations. state.request = request; state.filename = filename; // event wait on. waitobject = state.operationcomplete; // asynchronously stream file contents. request.begingetrequeststream( new asynccallback (endgetstreamcallback), state ); // block current thread until operations complete. waitobject.waitone(); // operations either completed or threw exception. if (state.operationexception != null) { throw state.operationexception; } else { console.writeline("the operation completed - {0}", state.statusdescription); } } private static void endgetstreamcallback(iasyncresult ar) { ftpstate state = (ftpstate) ar.asyncstate; stream requeststream = null; // end asynchronous phone call request stream. seek { requeststream = state.request.endgetrequeststream(ar); // re-create file contents request stream. const int bufferlength = 2048; byte[] buffer = new byte[bufferlength]; int count = 0; int readbytes = 0; filestream stream = file.openread(state.filename); { readbytes = stream.read(buffer, 0, bufferlength); requeststream.write(buffer, 0, readbytes); count += readbytes; } while (readbytes != 0); console.writeline ("writing {0} bytes stream.", count); // important: close request stream before sending request. requeststream.close(); // asynchronously response upload request. state.request.begingetresponse( new asynccallback (endgetresponsecallback), state ); } // homecoming exceptions main application thread. grab (exception e) { console.writeline("could not request stream."); state.operationexception = e; state.operationcomplete.set(); return; } } // endgetresponsecallback method // completes phone call begingetresponse. private static void endgetresponsecallback(iasyncresult ar) { ftpstate state = (ftpstate) ar.asyncstate; ftpwebresponse response = null; seek { response = (ftpwebresponse) state.request.endgetresponse(ar); response.close(); state.statusdescription = response.statusdescription; // signal main application thread // operation complete. state.operationcomplete.set(); } // homecoming exceptions main application thread. grab (exception e) { console.writeline ("error getting response."); state.operationexception = e; state.operationcomplete.set(); } } } }
c# .net windows c#-4.0
Comments
Post a Comment