connect to FTP server

G

Guest

I need to upload a file to an FTP server, can someone point me to some code
that connects to an FTP server and uploads a file?
 
K

Kevin Spencer

I might mention that these should be construed as examples. I had to write
an FTP client, and the Socket connection and related functionality is not
difficult at all. What *is* difficult is handling the various
network-related issues that can arise. For an FTP client to be robust, it
needs to have a lot of recovery functionality (retries, timers, etc) built
into it.

Another issue is that some of the format of the returned data, such as
directory listing and system status, is not standardized, and can be
difficult to parse.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 
G

Guest

I'll consider that as well, but all I need to do realy is upload the file
then check to confirm it got there.
 
M

Mark Rae

where? what link is it under? i went to all 3 and didn't see anything on
FTP

It was in an attachment to the reply. Your newsreader is probably set to
remove all attachments to posts - mine certainly is...
 
G

Guest

I'm using the code and I'm getting 'access denie to file c:\ftptest.txt"
file, or I get False returned. Do i need to do anything on my box other then
the code? I'm logged into the FTP server (via a gui) to see if my file is
getting uploaded and its not, also when I try and download a file I get
"access denied to c:\windows\system32\ftptest.txt"

whats missing
 
J

Juan T. Llibre

re:
when I try and download a file I get "access denied to c:\windows\system32\ftptest.txt"

You need to specify a directory which isn't security-conscious
as the destination for the uploaded files.

I'd *never* upload files to that directory.

Also, if you're getting "access denied", the account ASP.NET is running as
doesn't have enough permissions to access the file and/or directory.

Save the following code as "identity.aspx" and run the file.

identity.aspx
=========
<%@ Page Language="VB" %>
<%@ Import NameSpace = System.Security.Principal %>
<script runat="server">
Sub Page_Load()
Dim tmp As String = WindowsIdentity.GetCurrent.Name()
Label1.Text = tmp
End Sub
</script>
<html>
<head>
<title>WindowsIdentity.GetCurrent.Name()</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" Runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
======

Then, assign access permissions for the file/directory to the account which that file returns.

Remember to change the upload directory to an ad-hoc directory.
Don't use c:\windows\system32\ as your upload directory.




Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
G

Guest

I didn't specify that directory, when I was trying to pull down the file it
tried and dump it there. I have an upload directory I'm working on pulling
the files from
 
J

Juan T. Llibre

re:
I didn't specify that directory

Clearly.

That's why I suggested that you *do* specify it.


private void Page_Load(object sender, System.EventArgs e)
{
/*
* Set the "path" variable to the location where files are to be stored
*/
string path = "../docs";

HttpFileCollection files;
files = Page.Request.Files;
for(int index=0; index < files.AllKeys.Length; index++)
{
HttpPostedFile postedFile = files[index];
string fileName = null;
int lastPos = postedFile.FileName.LastIndexOf('\\');
fileName = postedFile.FileName.Substring(++lastPos);
//Check the file type through the extension
if(fileName.EndsWith("doc") || fileName.EndsWith("doc") ||
fileName.EndsWith("pdf") || fileName.EndsWith("pdf")
{
postedFile.SaveAs(MapPath(path + "/" + fileName));
}
}
}



Juan T. Llibre, asp.net MVP
aspnetfaq.com : http://www.aspnetfaq.com/
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
===================================
 
K

Kevin Spencer

I'll consider that as well, but all I need to do realy is upload the file
then check to confirm it got there.

To do that, you need to connect your Socket first, wait for a response from
the server, verify the response, send a PASV command to the server, open
your data connection, write the file to the server, close your data
connection, and then wait for a response from the server on your Command
connection.

Any of these can fail over a network, even though they rarely do. That's
what I was talking about. It can be a real nightmare trying to figure out
what went wrong if you don't account for every possibility.

As for your problems with the file upload, be sure that you specify the
absolute file path to your data connection for fetching the file, and a
root-relative path on the server for where you want to upload the file to.
In FTP, the root folder is the "lowest" folder you can access with your
account. This is referred to as "/" in FTP server paths. If you want to
upload the file to a folder underneath that folder, you would add the folder
name after the slash. For example, if the remote folder is:

C:\Inetpub\FtpRoot\folder1

It will be the folder "/" to your client. If you want to upload to

C:\Inetpub\FtpRoot\folder1\subfolder1

Your upload path will be
"/subfolder1/filename.xxx"

So, your command would be: "STOR /subfolder1/filename.xxx" + CRLF

Always be sure to check the response code, the first 3 characters in the
response. It will indicate whether or not the action succeeded. Your app may
not throw any exceptions, but fail to upload the file anyway.
--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Hard work is a medication for which
there is no placebo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

FTP problems 3
Upload and server FTP 2
managing an ftp server 1
FTP upload for aspx page 1
Http file upload 3
FTP Site 2
How to connect to Mainframe to download a file 3
Locating the ftp log file 3

Top