Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.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#_Ftp_Timeout - Fatal编程技术网

c#/使用ftp上传文件的更有效方法

c#/使用ftp上传文件的更有效方法,c#,ftp,timeout,C#,Ftp,Timeout,我正在上传一个带有c#的.txt文件,并且: client.Credentials=新的网络凭据(ftpU、ftpP); client.UploadFile(“这里是ftp服务器”、“STOR”、lfilepath) 有时它会抛出类似“系统错误”的错误 此.txt只是登录信息,其内容类似于User:Name At:2015/12/12 08:43 AM 是否有任何选项可以消除此错误?让ftp上传更有效?或者任何在互联网上保存登录信息的想法。尝试使用FtpWebRequest和WebRequest

我正在上传一个带有c#的.txt文件,并且:

client.Credentials=新的网络凭据(ftpU、ftpP);
client.UploadFile(“这里是ftp服务器”、“STOR”、lfilepath)

有时它会抛出类似“系统错误”的错误

此.txt只是登录信息,其内容类似于
User:Name At:2015/12/12 08:43 AM

是否有任何选项可以消除此错误?让ftp上传更有效?或者任何在互联网上保存登录信息的想法。

尝试使用FtpWebRequestWebRequestMethods.Ftp.UploadFile。下面是我们用于将文件从ZipArchive上传到FTP的一段代码(因此,如果您也需要,还可以选择创建目录)。在我测试过的所有方法中,它是最有效的一种

//// Get the object used to communicate with the server.
var request =
    (FtpWebRequest)
        WebRequest.Create("ftp://" + ftpServer + @"/" + remotePath + @"/" +
                          entry.FullName.TrimEnd('/'));

//// Determine if we are transferring file or directory
if (string.IsNullOrWhiteSpace(entry.Name) && !string.IsNullOrWhiteSpace(entry.FullName))
    request.Method = WebRequestMethods.Ftp.MakeDirectory;
else
    request.Method = WebRequestMethods.Ftp.UploadFile;

//// Try to transfer file
try
{
    //// This example assumes the FTP site uses anonymous logon.
    request.Credentials = new NetworkCredential(user, password);

    switch (request.Method)
    {
        case WebRequestMethods.Ftp.MakeDirectory:
            break;

        case WebRequestMethods.Ftp.UploadFile:
            var buffer = new byte[8192];
            using (var rs = request.GetRequestStream())
            {
                StreamUtils.Copy(entry.Open(), rs, buffer);
            }

            break;
    }
}
catch (Exception ex)
{
    //// Handle it!
    LogHelper.Error<FtpHelper>("Could not extract file from package.", ex);
}
finally
{
    //// Get the response from the FTP server.
    var response = (FtpWebResponse) request.GetResponse();

    //// Close the connection = Happy a FTP server.
    response.Close();
}
///获取用于与服务器通信的对象。
var请求=
(FtpWebRequest)
WebRequest.Create(“ftp://”+ftpServer++/+remotePath++/”+
entry.FullName.TrimEnd('/');
////确定是否正在传输文件或目录
if(string.IsNullOrWhiteSpace(entry.Name)和&!string.IsNullOrWhiteSpace(entry.FullName))
request.Method=WebRequestMethods.Ftp.MakeDirectory;
其他的
request.Method=WebRequestMethods.Ftp.UploadFile;
////尝试传输文件
尝试
{
////本例假设FTP站点使用匿名登录。
request.Credentials=新的网络凭据(用户、密码);
开关(请求方法)
{
案例WebRequestMethods.Ftp.MakeDirectory:
打破
案例WebRequestMethods.Ftp.UploadFile:
var buffer=新字节[8192];
使用(var rs=request.GetRequestStream())
{
StreamUtils.Copy(entry.Open(),rs,buffer);
}
打破
}
}
捕获(例外情况除外)
{
////处理它!
LogHelper.Error(“无法从包中提取文件。”,ex);
}
最后
{
////从FTP服务器获取响应。
var response=(FtpWebResponse)request.GetResponse();
////关闭FTP服务器的连接。
response.Close();
}
实现try..catch(异常示例)并用真实消息编辑您的帖子。