Created
October 30, 2017 21:30
-
-
Save zealott/443931661d081c6c6b1d6a89fbeeacd0 to your computer and use it in GitHub Desktop.
Form Data Write to File via FTP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public class CustomFormController : CMSPageDefaultController | |
| { | |
| [HttpPost] | |
| public ActionResult FormSubmit(FormCollection collection) | |
| { | |
| try | |
| { | |
| //set our string builder | |
| var csv = new StringBuilder(); | |
| //get our form elements | |
| var firstName = collection["first_name"]; | |
| var lastName = collection["last_name"]; | |
| var email = collection["email"]; | |
| var phoneNumber = collection["phone_number"]; | |
| var programName = collection["program_name"]; | |
| var pageName = collection["page_name"]; | |
| //format our csv line and add to csv | |
| var newLine = string.Format("{0},{1},{2},{3},{4}", firstName, lastName, email, phoneNumber, programName); | |
| csv.AppendLine(newLine); | |
| //get ftp values from web config | |
| string ftpUser = ConfigurationManager.AppSettings["FTPUser"]; | |
| string ftpPass = ConfigurationManager.AppSettings["FTPPW"]; | |
| string ftpLocation = ConfigurationManager.AppSettings["FTPLocation"]; | |
| string address = @"ftp://" + ftpLocation + "/" + pageName + | |
| DateTime.Now.ToString("yyyy-MM-ddTHH-mm-ss") + ".txt"; | |
| // Get the object used to communicate with the server. | |
| FtpWebRequest request = (FtpWebRequest)WebRequest.Create(address); | |
| request.Method = WebRequestMethods.Ftp.UploadFile; | |
| // set the credentials | |
| request.Credentials = new NetworkCredential(ftpUser, ftpPass); | |
| byte[] fileContents = Encoding.UTF8.GetBytes(csv.ToString()); | |
| request.ContentLength = fileContents.Length; | |
| Stream requestStream = request.GetRequestStream(); | |
| requestStream.Write(fileContents, 0, fileContents.Length); | |
| requestStream.Close(); | |
| FtpWebResponse response = (FtpWebResponse)request.GetResponse(); | |
| response.Close(); | |
| } | |
| catch(Exception e) | |
| { | |
| //get error url from web config and redirect | |
| string errorUrl = ConfigurationManager.AppSettings["ErrorURL"]; | |
| return Redirect(errorUrl); | |
| } | |
| //on success redirect to success url in this version it is from the | |
| //form, could be from the web config | |
| var redirectUrl = collection["redirect_url"]; | |
| return Redirect(redirectUrl); | |
| } | |
| protected override ActionResult handleCmsPageRequestWithRemainingPath(CMSPageRequest pageRequest, string remainingPath) | |
| { | |
| ViewData["RemaingPath"] = remainingPath; | |
| return View(pageRequest); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| The task is to collect data from a form and write each submission to it's own | |
| uniquely identified text file using ftp protocol. To accomplish this we need to | |
| set some variables on the webconfig to allow the customer flexibility with ftp | |
| credentials and location. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| string thisUrl = _Functions.ThisPageUrl(Model as ICMSPage, Url) + Model.ID + ".xml"; | |
| string pageName = Model.GetAttributeValue("Name"); | |
| //Url.Action("ControllerActionName", "ControllerName") | |
| <form class="contact" method="post" action="@Url.Action("FormSubmit", "LandingPageForm")"> | |
| <input type="text" placeholder="first name" name="first_name" required> | |
| <input type="text" placeholder="last name" name="last_name" required> | |
| <input type="text" placeholder="email" name="email" required> | |
| <input type="text" placeholder="phone number" name="phone_number"> | |
| <input type="text" placeholder="program name" name="program_name" required> | |
| <input type="hidden" name="page_name" value="@pageName" /> | |
| <input type="hidden" name="redirect_url" value="@thisUrl" /> | |
| <input type="submit" placeholder="submit"> | |
| </form> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <appSettings> | |
| <add key="ErrorURL" value="errorUrlValue"/> | |
| <add key="FTPUser" value="ftpUserNameValue"/> | |
| <add key="FTPPW" value="ftpPasswordValue"/> | |
| <add key="FTPLocation" value="ftpAddressValue"/> | |
| </appSettings> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment