c# - cannot upload files to ftp asp.net -
i have problem creating files on ftp production server. following code working fine on development server, when move code production server on site have bought hosting gives error
code
string fullname = convert.tostring(txtbx_fullname.text); if (fu_img.hasfile) { string ftpurl = ""; httppostedfile userpostfile = fu_img.postedfile; string uploadurl = @"ftp://abc/upload/u" + fullname; string uploadfilename = path.getfilename(fu_img.filename); uploadfilename = "image." + uploadfilename.split('.')[1]; stream streamobj = userpostfile.inputstream; byte[] buffer = new byte[userpostfile.contentlength]; streamobj.read(buffer, 0, buffer.length); streamobj.close(); streamobj = null; // ftpwebresponse createforderresponse; if (!getallfileslist(uploadurl, "xyz", "******")) { ftpwebrequest ftp = (ftpwebrequest)ftpwebrequest.create(uploadurl); ftp.method = webrequestmethods.ftp.makedirectory; ftp.credentials = new networkcredential("xyz", "******"); ftpwebresponse response = (ftpwebresponse)ftp.getresponse(); } //if (createforderresponse.statuscode == ftpstatuscode.pathnamecreated) ftpurl = string.format("{0}/{1}", uploadurl, uploadfilename); ftpwebrequest requestobj = ftpwebrequest.create(ftpurl) ftpwebrequest; requestobj.keepalive = false; requestobj.usebinary = true; requestobj.method = webrequestmethods.ftp.uploadfile; requestobj.credentials = new networkcredential("xxxx", "xxxxxx"); stream requeststream = requestobj.getrequeststream(); <----- error requeststream.write(buffer, 0, buffer.length); requeststream.flush(); requeststream.close(); requestobj = null;
error
stack trace
[webexception: remote server returned error: (550) file unavailable (e.g., file not found, no access).] system.net.ftpwebrequest.syncrequestcallback(object obj) +341 system.net.ftpwebrequest.requestcallback(object obj) +23 system.net.commandstream.dispose(boolean disposing) +19 system.io.stream.close() +20 system.io.stream.dispose() +10 system.net.connectionpool.destroy(pooledstream pooledstream) +188 system.net.connectionpool.putconnection(pooledstream pooledstream, object owningobject, int32 creationtimeout, boolean canreuse) +118 system.net.ftpwebrequest.finishrequeststage(requeststage stage) +655 system.net.ftpwebrequest.getrequeststream() +795 mentormentee.signup.signup.btn_preview_click(object sender, eventargs e) +954 system.eventhandler.invoke(object sender, eventargs e) +0 system.web.ui.webcontrols.button.onclick(eventargs e) +9553178 system.web.ui.webcontrols.button.raisepostbackevent(string eventargument) +103 system.web.ui.webcontrols.button.system.web.ui.ipostbackeventhandler.raisepostbackevent(string eventargument) +10 system.web.ui.page.raisepostbackevent(ipostbackeventhandler sourcecontrol, string eventargument) +13 system.web.ui.page.raisepostbackevent(namevaluecollection postdata) +35 system.web.ui.page.processrequestmain(boolean includestagesbeforeasyncpoint, boolean includestagesafterasyncpoint) +1724
what found false attribute keepalive, did not work. going wrong?
the following solution worked out me.
private bool ftpupload(fileupload file, string ftpserver, string username, string ftppass, string domainname = "") { // ftp://domain\user:password@ftpserver/url-path // if member of domain, "ftp://domain-name\username:password@url-path" may fail because backslash (\) sent in literal character , internet explorer incorrectly looks file instead of parsing web address. changing backslash (\) in domain-name\username domainname%5cusername works correctly. try { string ftpaddres; if (domainname != string.empty) ftpaddres = "ftp://" + domainname + @"%5c" + username + ":" + ftppass + "@" + ftpserver + "/" + file.filename; else ftpaddres = "ftp://" + username + ":" + ftppass + "@" + ftpserver + "/" + file.filename; using (var webclient = new system.net.webclient()) { webclient.uploaddata(new uri(ftpaddres), file.filebytes); } } catch (exception e) { throw new exception(e.message, e); } return true; }
thanks @freelancer link posted in comment.
Comments
Post a Comment