简体   繁体   中英

Error when upload files on ftp server

Please help me, I have a very big problem.

I want to upload a file on godaddy ftp server using asp.net c#. When I run the application in visual studio the file is created successfully on ftp server but, when I create this file using a url such as (www.domain/page.aspx) directly I get this error (using asp.net 4.0):

Unable to connect to the remote server

And I get this error when I use asp.net 3.5:

Request for the permission of type 'System.Net.WebPermission, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Please help me.

Make sure you are using credentials that have access to FTP. Also, your url would probably be something like ftp://www.domain.com/

Here's some FTP code that I've used in the past.

public class FTP
{
    private String Username { get; set; }
    private String Password { get; set; }
    private String Host { get; set; }
    private Int32 Port { get; set; }

    public FTP(String username, String password, String host, Int32 port)
    {
        Username = username;
        Password = password;
        Host = host;
        Port = port;
    }

    private Uri BuildServerUri(string Path)
    {
        return new Uri(String.Format("ftp://{0}:{1}/{2}", Host, Port, Path));
    }

    /// <summary>
    /// Upload a byte[] to the FTP server
    /// </summary>
    /// <param name="path">Path on the FTP server (upload/myfile.txt)</param>
    /// <param name="Data">A byte[] containing the data to upload</param>
    /// <returns>The server response in a byte[]</returns>
    private byte[] UploadData(string path, byte[] Data)
    {
        // Get the object used to communicate with the server.
        WebClient request = new WebClient();

        try
        {
            // Logon to the server using username + password
            request.Credentials = new NetworkCredential(Username, Password);
            return request.UploadData(BuildServerUri(path), Data);
        }
        finally
        {
            if (request != null)
                request.Dispose();
        }
    }

    /// <summary>
    /// Load a file from disk and upload it to the FTP server
    /// </summary>
    /// <param name="ftppath">Path on the FTP server (/upload/myfile.txt)</param>
    /// <param name="srcfile">File on the local harddisk to upload</param>
    /// <returns>The server response in a byte[]</returns>
    public byte[] UploadFile(string ftppath, string srcfile)
    {
        // Read the data from disk
        FileStream fs = new FileStream(srcfile, FileMode.Open);
        try
        {
            byte[] FileData = new byte[fs.Length];

            int numBytesToRead = (int)fs.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
            {
                // Read may return anything from 0 to numBytesToRead.
                int n = fs.Read(FileData, numBytesRead, numBytesToRead);

                // Break when the end of the file is reached.
                if (n == 0) break;

                numBytesRead += n;
                numBytesToRead -= n;
            }

            numBytesToRead = FileData.Length;

            // Upload the data from the buffer
            return UploadData(ftppath, FileData);
        }
        finally
        {
            if (fs != null)
                fs.Close();
            if (fs != null)
                fs.Dispose();
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM