2

In my Web Application I need to give facility to the user to browse their FTP Location, as of File Browse of Asp.Net.

To do so, I have used OpenFileDialog of Windows Forms. It works for any local location, but it does not work for any ftp location.

Below is my Code.

    protected void BtnOpenFileDialog_Click(object sender, EventArgs e)
    {
        var t = new Thread(SelectFolder);
        t.IsBackground = true;
        t.SetApartmentState(ApartmentState.STA);
        t.Start();
    }

    private void SelectFolder()
    {
        Stream myStream = null;
        OpenFileDialog openFileDialog1 = new OpenFileDialog();

        openFileDialog1.InitialDirectory = "ftp://username:password@ftpidaddress/";
        openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";
        openFileDialog1.FilterIndex = 2;
        openFileDialog1.RestoreDirectory = false;

        if (openFileDialog1.ShowDialog() == DialogResult.OK)
        {
            try
            {
                if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    using (myStream)
                    {
                        // code to read the stream here.
                    }
                }
            }
            catch (Exception ex)
            {
                // exception handing here
            }
        }
    }

Please suggest me if I am doing anything wrong in it.. Or suggest me if any other ways to do so.

Daniel Fischer
  • 181,706
  • 17
  • 308
  • 431
meghana
  • 907
  • 1
  • 20
  • 45
  • I am sorry I did not get it, you used `OpenFileDialog` in web application?? – Furqan Hameedi Jun 27 '12 at 14:06
  • 1
    You need to manage FTP protocol with [FTP request](http://msdn.microsoft.com/en-us/library/system.net.ftpwebrequest.aspx) instead of your dialog hack. – Agnius Vasiliauskas Jun 27 '12 at 14:12
  • @Furqan yes , I am using OpenFileDialog with Asp.net , by adding above code in Asp.net C# application , it works fine except it do not allow ftp location to browse . – meghana Jun 27 '12 at 14:23
  • @0x69 , we need to give user as folder view experience. and i found OpenFileDialog can be good solution, i guess this is not hack. – meghana Jun 27 '12 at 14:24
  • 1
    I mean because dialog don't supports FTP protocol you need to implement your own FTP folder view control which uses `System.Net.FtpWebRequest` class. Or try to look on internet for already implemented FTP browsers. – Agnius Vasiliauskas Jun 27 '12 at 14:34
  • @meghana, you cannot use `OpenFileDialog` in web applications, since this is a windows application control, though you can create it through your code, it shall display the dialog on server machine.check `http://xceed.com/FTP_NET_Demo.html` – Furqan Hameedi Jun 27 '12 at 14:36
  • @0x69 , can you have any suggestions for FTP Browsers that already implemented and available? – meghana Jun 28 '12 at 05:14
  • @Furqan thanks for reply , but i need free source or i may implement it... but if it is available free then good. – meghana Jun 28 '12 at 05:16
  • 1
    @meghana You can start from this [codeproject example](http://www.codeproject.com/Articles/29851/OpenFileDialog-for-FTP) – Agnius Vasiliauskas Jun 28 '12 at 06:30

0 Answers0