在文件上载单击事件中动态创建目录和子目录到Windows FTP服务器,并将上载的文件保存到该子目录中。 [英] Create directory and subdirectory dynamically into windows FTP server on file upload click event and save the uploaded file into that subdirectory.

查看:77
本文介绍了在文件上载单击事件中动态创建目录和子目录到Windows FTP服务器,并将上载的文件保存到该子目录中。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个asp.net网络表单,其中包含两个名为FirstName和LastName的文本框。我使用文件上传和通用处理程序从同一个aspx表单上传员工的文档。我的ftp路径是ftp://192.168.1.10/,我想用用户输入的FirstName和LastName值动态创建一个目录。

任何人都可以帮助我???



我尝试过:



Generic Handler代码



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代码






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>

推荐答案

当我在给定页面上有文件上传功能时,我处理机械部件(在控制器中创建文件夹,保存文件等)。为此,您必须有一个回送到控制器的输入按钮。这是最简单的方法。



如果你要将文件保存到远程FTP服务器,你将不得不建立ftp连接,并执行适当的ftp操作来做你做的事情想。如果您只是上传到网站上的文件夹,您只需要建立保存文件的完全限定路径,并使用文件系统方法来管理文件夹。
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).

这篇关于在文件上载单击事件中动态创建目录和子目录到Windows FTP服务器,并将上载的文件保存到该子目录中。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆