How to improve the Performance of FtpWebRequest?

后端 未结 18 1170
忘掉有多难
忘掉有多难 2020-11-28 04:46

I have an application written in .NET 3.5 that uses FTP to upload/download files from a server. The app works fine but there are performance issues:

  1. It take

18条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-28 05:13

    Try this below code, you will get better performence:

    private void Upload144_Click(object sender, EventArgs e)
    {
        OpenFileDialog fileobj = new OpenFileDialog();
        fileobj.InitialDirectory = "C:\\";
        //fileobj.Filter = "Video files (*.mp4)";
        //fileobj.ShowDialog();
    
        if (fileobj.ShowDialog() == DialogResult.OK)
        {
            if (fileobj.CheckFileExists)
            {
                string test = Properties.Settings.Default.Connection;
                SqlConnection con = new SqlConnection(test);
                con.Open();
                string correctfilename = System.IO.Path.GetFileName(fileobj.FileName);
                SqlCommand cmd = new SqlCommand("Insert into Path(ID,Path5) VALUES   ((select isnull(MAX(id),0) + 1 from Path),'\\Videos\\" + correctfilename + "')", con);
    
                cmd.ExecuteNonQuery();
    
                string path = Application.StartupPath.Substring(0, Application.StartupPath.Length - 10);
                con.Close();
    
                //For Progressbar
                DataTable dt = new DataTable();
           //   SqlDataAdapter da = new SqlDataAdapter(cmd);
           //   da.Fill(dt);
    
                timer5.Enabled = true;
    
                // FOR FtpServer File Upload::
                string uploadfile = fileobj.FileName;
                string uploadFileName = new FileInfo(uploadfile).Name;
    
                string uploadUrl = "ftp://ftp.infotech.com/";
                FileStream fs = new FileStream(uploadfile, FileMode.Open, FileAccess.Read);
                try
                {
                    long FileSize = new FileInfo(uploadfile).Length; // File size of file being uploaded.
                    Byte[] buffer = new Byte[FileSize];
    
                    fs.Read(buffer, 0, buffer.Length);
    
                    fs.Close();
                    fs = null;
                    string ftpUrl = string.Format("{0}/{1}", uploadUrl, uploadFileName);
                    FtpWebRequest requestObj = FtpWebRequest.Create(ftpUrl) as FtpWebRequest;
                    requestObj.Method = WebRequestMethods.Ftp.UploadFile;
                    requestObj.Credentials = new NetworkCredential("test@sample.com", "test@123");
                    Stream requestStream = requestObj.GetRequestStream();
                    requestStream.Write(buffer, 0, buffer.Length);
    
                    requestStream.Flush();
                    requestObj = null;
                }
                catch (Exception ex)
                {
                    //MessageBox.Show("File upload/transfer Failed.\r\nError Message:\r\n" + ex.Message, "Succeeded", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
        }
    }
    

提交回复
热议问题