Drag and drop FTP "Drop Box"...

Error when clicking the Upload button...

Page 1 of 1

4 Replies - 7213 Views - Last Post: 25 March 2009 - 04:34 PM Rate Topic: -----

#1 webopolis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 09

Drag and drop FTP "Drop Box"...

Posted 23 March 2009 - 01:42 PM

I know I'm close, but not quite there. I'm getting this error when I run it: "The requested URI is invalid for this FTP command.", and can't quite figure out how to make it work. Ultimately, if I can get it working at this stage, I'm going to have the host and credentials come from an XML file.

Thanks for any help...

Here's my code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace DropBox
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		public System.IO.FileInfo currentFile;

		private void listBox1_DragEnter_1(object sender, DragEventArgs e)
		{
			// make sure they user is actually dropping files (not text or anything else)
			if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
				// allow them to continue
				// (without this, the cursor stays a "NO" symbol
				e.Effect = DragDropEffects.All;
		}

		private void listBox1_DragDrop_1(object sender, DragEventArgs e)
		{
			// transfer the filenames to a string array
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			//bool wrongFile = new bool();

			// loop through the string array, adding each filename to the ListBox
			foreach (string file in files)
			{
				listBox1.Items.Add(file);
			}
		}

		public void ftpfile(string ftpfilepath, string inputfilepath)
		{
			string ftphost = "www.mydomain.com";
			//here correct hostname or IP of the ftp server to be given

			string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
			FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
			ftp.Credentials = new NetworkCredential("myusername", "mypassword");
			//userid and password for the ftp server to given

			ftp.KeepAlive = true;
			ftp.UseBinary = true;
			ftp.Method = WebRequestMethods.Ftp.UploadFile;
			FileStream fs = File.OpenRead(inputfilepath);
			byte[] buffer = new byte[fs.Length];
			fs.Read(buffer, 0, buffer.Length);
			fs.Close();
			Stream ftpstream = ftp.GetRequestStream();   //<-- ERROR OCCURS HERE
			ftpstream.Write(buffer, 0, buffer.Length);
			ftpstream.Close();
		}

		private void button1_Click(object sender, EventArgs e)
		{
			for (int x = 0; x < listBox1.Items.Count; x++)
			{
				foreach (string f in listBox1.Items)
				{
					ftpfile("/", f);
				}
			}
			
		}

	}
}


This post has been edited by webopolis: 23 March 2009 - 01:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Drag and drop FTP "Drop Box"...

#2 webopolis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 09

Re: Drag and drop FTP "Drop Box"...

Posted 25 March 2009 - 06:50 AM

Ok. I resolved that error, but now I get another error: "The remote server returned an error: (501) Syntax error in parameters or arguments."

I cannot find the problem with my syntax. If someone can help, offer a clue, etc., I'd appreciate it.

Thanks.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Net;
using System.Text;
using System.Windows.Forms;
using System.Collections.ObjectModel;

namespace DropBox
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		public System.IO.FileInfo currentFile;

		private void listBox1_DragEnter_1(object sender, DragEventArgs e)
		{
			// make sure they user is actually dropping files (not text or anything else)
			if (e.Data.GetDataPresent(DataFormats.FileDrop, false) == true)
				// allow them to continue
				// (without this, the cursor stays a "NO" symbol
				e.Effect = DragDropEffects.All;
		}

		private void listBox1_DragDrop_1(object sender, DragEventArgs e)
		{
			// transfer the filenames to a string array
			string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
			//bool wrongFile = new bool();

			// loop through the string array, adding each filename to the ListBox
			foreach (string file in files)
			{
				listBox1.Items.Add(file);
				MessageBox.Show(System.IO.Path.GetFileName(file));
			}
		}

		private void button1_Click(object sender, EventArgs e)
		{
			for (int x = 0; x < listBox1.Items.Count; x++)
			{
				foreach (string f in listBox1.Items)
				{
					string fileName = System.IO.Path.GetFileName(f);
					ftpfile(@System.IO.Path.GetFileName(fileName), @f);
				}
			}
		}

		public void ftpfile(string ftpfilepath, string inputfilepath)
		{
			string ftphost = "www.myftphost.com";
			//here correct hostname or IP of the ftp server to be given

			string ftpfullpath = "ftp://" + ftphost + "/" + ftpfilepath;

			//MessageBox.Show(ftpfullpath);

			FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
			ftp.Credentials = new NetworkCredential("myusername", "mypassword");
			//userid and password for the ftp server to given

			ftp.KeepAlive = true;
			ftp.UsePassive = true;
			ftp.UseBinary = true;
			ftp.Method = WebRequestMethods.Ftp.UploadFile;
			FileStream fs = File.OpenRead(inputfilepath);
			byte[] buffer = new byte[fs.Length];
			fs.Read(buffer, 0, buffer.Length);
			fs.Close();
			Stream ftpstream = ftp.GetRequestStream();
			ftpstream.Write(buffer, 0, buffer.Length);
			ftpstream.Close();
		}
	}
}


Was This Post Helpful? 0
  • +
  • -

#3 webopolis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 09

Re: Drag and drop FTP "Drop Box"...

Posted 25 March 2009 - 11:34 AM

Incredible. Well, since no one is coming to my rescue, and I have no intention of sitting here waiting for someone to do so, I have yet again resolved another of my problems.

I simply need to change:

ftp.UsePassive = true;


to:

ftp.UsePassive = false;


Now on to the next task. How do I get the upload to run in the background so I can add a progressbar, and remove each item from the list as it completes? PLEASE help me with this. I have no idea where to start.

I've had lots of views, but no replies. Thank you for at least looking.
Was This Post Helpful? 0
  • +
  • -

#4 mario.popescu  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 25-March 09

Re: Drag and drop FTP "Drop Box"...

Posted 25 March 2009 - 01:55 PM

Hi!

Can you please tell me what option did you choose for the server side programming?

View Postwebopolis, on 25 Mar, 2009 - 10:34 AM, said:

Incredible. Well, since no one is coming to my rescue, and I have no intention of sitting here waiting for someone to do so, I have yet again resolved another of my problems.

I simply need to change:

ftp.UsePassive = true;


to:

ftp.UsePassive = false;


Now on to the next task. How do I get the upload to run in the background so I can add a progressbar, and remove each item from the list as it completes? PLEASE help me with this. I have no idea where to start.

I've had lots of views, but no replies. Thank you for at least looking.

Was This Post Helpful? 0
  • +
  • -

#5 webopolis  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 22-March 09

Re: Drag and drop FTP "Drop Box"...

Posted 25 March 2009 - 04:34 PM

No server side programming. It's sending to an FTP server. Gene6 FTP Server, to be exact.

Why do you ask?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1