Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ftp服务器上上载文件时出错_C#_Asp.net - Fatal编程技术网

C# 在ftp服务器上上载文件时出错

C# 在ftp服务器上上载文件时出错,c#,asp.net,C#,Asp.net,请帮帮我,我有一个很大的问题 我想使用asp.net c#在godaddy ftp服务器上上载文件。当我在visual studio中运行应用程序时,文件在ftp服务器上成功创建,但当我使用url(如(www.domain/page.aspx)直接创建此文件时,我收到此错误(使用asp.net 4.0): 无法连接到远程服务器 我在使用asp.net 3.5时出现以下错误: 请求“System.Net.WebPermission,System,Version=2.0.0.0,Culture=ne

请帮帮我,我有一个很大的问题

我想使用asp.net c#在godaddy ftp服务器上上载文件。当我在visual studio中运行应用程序时,文件在ftp服务器上成功创建,但当我使用url(如(www.domain/page.aspx)直接创建此文件时,我收到此错误(使用asp.net 4.0):

无法连接到远程服务器

我在使用asp.net 3.5时出现以下错误:

请求“System.Net.WebPermission,System,Version=2.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”类型的权限失败


请帮助我。

确保您使用的凭据可以访问FTP。此外,您的url可能类似于

这是我过去使用过的一些FTP代码

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();
        }
    }
}
公共类FTP
{
私有字符串用户名{get;set;}
私有字符串密码{get;set;}
私有字符串主机{get;set;}
专用Int32端口{get;set;}
公共FTP(字符串用户名、字符串密码、字符串主机、Int32端口)
{
用户名=用户名;
密码=密码;
主机=主机;
端口=端口;
}
私有Uri BuildServerUri(字符串路径)
{
返回新的Uri(String.Format(“ftp://{0}:{1}/{2}”,主机,端口,路径));
}
/// 
///将字节[]上载到FTP服务器
/// 
///FTP服务器上的路径(upload/myfile.txt)
///包含要上载的数据的字节[]
///以字节[]表示的服务器响应
私有字节[]上传数据(字符串路径,字节[]数据)
{
//获取用于与服务器通信的对象。
WebClient请求=新建WebClient();
尝试
{
//使用用户名+密码登录到服务器
request.Credentials=新的网络凭据(用户名、密码);
返回请求.UploadData(BuildServerUri(路径),数据);
}
最后
{
if(请求!=null)
request.Dispose();
}
}
/// 
///从磁盘加载文件并将其上载到FTP服务器
/// 
///FTP服务器上的路径(/upload/myfile.txt)
///要上载的本地硬盘上的文件
///以字节[]表示的服务器响应
公共字节[]上传文件(字符串ftppath,字符串srcfile)
{
//从磁盘读取数据
FileStream fs=newfilestream(srcfile,FileMode.Open);
尝试
{
字节[]文件数据=新字节[fs.Length];
int numBytesToRead=(int)fs.Length;
int numBytesRead=0;
而(numBytesToRead>0)
{
//Read可能返回0到numBytesToRead之间的任何内容。
int n=fs.Read(FileData,numBytesRead,numBytesToRead);
//到达文件末尾时中断。
如果(n==0)中断;
numBytesRead+=n;
numBytesToRead-=n;
}
numBytesToRead=FileData.Length;
//从缓冲区上传数据
返回上传数据(ftppath,FileData);
}
最后
{
如果(fs!=null)
fs.Close();
如果(fs!=null)
fs.Dispose();
}
}
}

我使用此代码上载文件//FtpWebRequest FTPReq1=(FtpWebRequest)FtpWebRequest.Create(“FileUpload1.FileName”);//FTPReq1.UseBinary=true;//FTPReq1.Credentials=new NetworkCredential(“用户名”,“通过”);//FTPReq1.Method=WebRequestMethods.Ftp.UploadFile;//FtpWebResponse fres1=(FtpWebResponse)FTPReq1.GetResponse();感谢您的帮助,但我遇到了相同的错误(无法连接到远程服务器)您可以通过命令行连接和传输文件吗?我知道命令行,但我的项目需要使用url请求的asp.net创建文件和文件夹,谢谢您的帮助。我想知道您是否能够通过命令行验证它是否正常工作,以排除非编程问题。当我从m visual studio 2010,但当从url请求时,相同的代码不起作用