Click here to Skip to main content
15,910,121 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a asp.net web form with 2 textboxes named "FirstName" and "LastName". I am uploading the documents for the employee from the same aspx form using file upload and generic handler. I ftp path is "ftp://192.168.1.10/" and I want to create a directory dynamically with the FirstName and LastName value entered by user.
Can anyone help me???

What I have tried:

Generic Handler code

int count = 0;
            bool IsFileExist = true;
            string fileName = null;
            string FirstName = oEntity.FirstName;
            string LastName = oEntity.LastName;
            //if (context.Session["mode"] != null && context.Session["mode"] == "ajax")
            //{
            //    string FirstName = context.Session(["FirstName"]) ;
            //    string LastName = context.Session["LastName"];
            //}
            //if (Request.QueryString["mode"] != null && Request.QueryString["mode"] == "ajax")
            //{
            //    //Saving the variables in session. Variables are posted by ajax.
            //    Session["FirstName"] = Request.Form["FirstName"] ?? "";
            //    Session["LastName"] = Request.Form["LastName"] ?? "";
            //}

            
            if (context.Request.Files.Count > 0)
            {
                HttpFileCollection files = context.Request.Files;
                for (int i = 0; i < files.Count; i++)
                {
                    HttpPostedFile file = files[i];

                    //FTP Server URL.
                    string ftp = "ftp://192.168.1.10/";

                    //FTP Folder name. Leave blank if you want to upload to root folder.
                    string ftpFolder = "Employee_Attachments/" + FirstName + LastName;
                    DirectoryInfo MainDirectory = new DirectoryInfo(ftpFolder);
                    DirectoryInfo SubDirectory = MainDirectory.CreateSubdirectory(FirstName + LastName);
                    //Directory.CreateDirectory = (FirstName + "_" + LastName);
                    
                    byte[] fileBytes = null;

                    //Read the FileName and convert it to Byte array.
                    fileName = Path.Combine(Path.GetFileName(file.FileName));
                    while (IsFileExist == true)
                    {   
                        IsFileExist = CheckIfFileExistsOnServer(fileName);
                        if (IsFileExist == true)
                        {
                            string extension = Path.GetExtension(fileName);
                            fileName = fileName.Substring(0, fileName.IndexOf(".") + 0);
                            if (fileName.Contains('('))
                            {
                                fileName = fileName.Substring(0, fileName.IndexOf("(") + 0);
                            }

                            string withoutextension = Path.GetFileNameWithoutExtension(fileName);
                            string po = withoutextension + '(' + count++ + ')';
                            fileName = po + extension;
                        }
                    }

                    using (StreamReader fileStream = new StreamReader(file.InputStream))
                    { 
                        fileBytes = Encoding.UTF8.GetBytes(fileStream.ReadToEnd());
                        fileStream.Close();
                    }
                    try
                    {
                        //Create FTP Request.
                        //FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + ftpFolder + fileName);
                        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftp + MainDirectory + SubDirectory + fileName);
                        request.Method = WebRequestMethods.Ftp.UploadFile;

                        //Enter FTP Server credentials.
                        request.Credentials = new NetworkCredential("Administrator", "Support24x7");
                        request.ContentLength = fileBytes.Length;
                        request.UsePassive = true;
                        request.UseBinary = true;
                        request.ServicePoint.ConnectionLimit = fileBytes.Length;
                        request.EnableSsl = false;

                        using (Stream requestStream = request.GetRequestStream())
                        {
                            requestStream.Write(fileBytes, 0, fileBytes.Length);
                            requestStream.Close();
                        }
                        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
                        // fileName += fileName;

                        response.Close();
                    }
                    catch (WebException ex)
                    {
                        throw new Exception((ex.Response as FtpWebResponse).StatusDescription);
                    }
                }
                context.Response.StatusCode = 200;
                context.Response.ContentType = "text/plain";
                context.Response.Write(fileName);
            }



Aspx code

<div id="change">
                <div class="col-md-12">
                    <div class="col-md-4">
                        <div class="col-md-4">
                            <label id="lblFirstName" class="control-label">FirstName</label>
                        </div>
                        <div class="col-md-8">
                            <input id="txtFirstName" type="text" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="col-md-4">
                            <label id="lblMiddleName" class="control-label">MiddleName</label>
                        </div>
                        <div class="col-md-8">
                            <input id="txtMiddleName" type="text" class="form-control" />
                        </div>
                    </div>
                    <div class="col-md-4">
                        <div class="col-md-4">
                            <label id="lblLastName" class="control-label">LastName</label>
                        </div>
                        <div class="col-md-8">
                            <input id="txtLastName" type="text" class="form-control" />
                        </div>
                    </div>
                </div></div><pre> <div class="form-group vspace">
                            <div class="col-lg-12">
                                <div class="col-md-10">
                                    <div class="col-md-3">
                                        <label for="fupload" />
                                        <input id="fupload" type="file" />
                                    </div>
                                    <div class="col-md-7">
                                        <input id="btnUpload" type="button" onclick="" value="Upload" class="btncan" />
                                    </div>
                                </div>
                            </div>
                        </div>
Posted
Updated 8-Feb-18 4:39am
Comments
ZurdoDev 7-Feb-18 11:08am    
What exactly is your question?
Prafful007 8-Feb-18 0:36am    
My question is as follows....
When i click on file upload control-> select a file -> then click on upload button, I want a folder to be created automatically on ftp server. The folder created should be named with the text given in two textbox.
for eg:- if i type name "Prafful" in txtFirstName(id of textbox) and "Coder" in txtLastName then the folder created on the ftp server will be of name Prafful_Coder. The file uploaded should get saved into this folder.
Prafful007 8-Feb-18 0:38am    
All the functionality should take place on upload button click event.
ZurdoDev 8-Feb-18 7:59am    
So, your question really is, how do you pass more information during the file upload. And that depends on what file upload control you are using.
Prafful007 8-Feb-18 11:51am    
yes exactly!!!

1 solution

When I have file upload functionality on a given page, I handle the mechanical parts (creating folders, saving the file, etc) in the controller. In order to do that, you have to have an input button that posts back to the controller. That's the easiest way to do it.

If you're saving a file to a remote FTP server, you're going to have to establish the ftp connection, and perform appropriate ftp actions to do what you want. If you're just uploading to a folder on the web site, you simply have to establish the fully qualified path in which to save the file, and use file system methods to manage the folder(s).
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900