这是方法:
在这种情况下我也使用f.TargetFolder f.TargetFolder包含:ftpmytestdir
变量UploadPath包含:
ftp.newsxpressmedia.com/ftpmytestdir/clean_radar_image.png
然后就会这样做:
UploadPath = "ftp://" + UploadPath;
之后,UploadPath将是:
ftp://ftp.newsxpressmedia.com/ftpmytestdir/clean_radar_image.png
文件是string []类型 例如在txf中我有test.png
我看到了文件大小,除了Stream之外,所有内容都没有例外。
我使用断点并移动直到第78行:
using (Stream requestStream = request.GetRequestStream())
在这一行上,它跳转到捕获区域并显示异常。
private void StringArrayUploadFiles(object sender, DoWorkEventArgs e)
{
try
{
foreach (string txf in files)
{
string fn = txf;
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));
if (!UploadPath.ToLower().StartsWith("ftp://"))
UploadPath = "ftp://" + UploadPath;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
request.UseBinary = true;
request.UsePassive = f.Passive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.Credentials = new NetworkCredential(f.Username, f.Password);
long FileSize = new FileInfo(fn).Length;).Length;
string FileSizeDescription = GetFileSize(FileSize);
int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.Open(fn, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int BytesRead = fs.Read(Buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;
requestStream.Write(Buffer, 0, BytesRead);
SentBytes += BytesRead;
string SummaryText = String.Format("Transferred {0} / {1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes / (decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
}
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
ConnectionError = "Error: Please check the ftp address";
break;
case WebExceptionStatus.Timeout:
ConnectionError = "Error: Timout Request";
break;
}
}
}
这是异常消息:远程服务器返回错误:(550)文件不可用(例如,找不到文件,没有访问权限。)
System.Net.WebException was caught
HResult=-2146233079
Message=The remote server returned an error: (550) File unavailable (e.g., file not found, no access).
Source=System
StackTrace:
at System.Net.FtpWebRequest.CheckError()
at System.Net.FtpWebRequest.SyncRequestCallback(Object obj)
at System.IO.Stream.Close()
at System.Net.ConnectionPool.Destroy(PooledStream pooledStream)
at System.Net.ConnectionPool.PutConnection(PooledStream pooledStream, Object owningObject, Int32 creationTimeout, Boolean canReuse)
at System.Net.FtpWebRequest.FinishRequestStage(RequestStage stage)
at System.Net.FtpWebRequest.GetRequestStream()
at FTP_ProgressBar.FtpProgress.StringArrayUploadFiles(Object sender, DoWorkEventArgs e) in c:\ftp_progressbar\FTP_ProgressBar\FtpProgress.cs:line 78
InnerException:
答案 0 :(得分:1)
从异常中看起来很清楚 - (550)文件不可用(例如,找不到文件,没有访问权限。)
550是FTP错误代码,表示该文件不可用。 FTP不够复杂,无法提供更多细节。
由于我无法访问FTP服务器或使用的凭据,并且假设您正在尝试上传文件,而不是下载,我猜测帐户您使用的服务器上没有写入权限,因此您无法创建该文件。由于某种原因,文件名也可能无效。
在这种情况下进行的一项很好的测试是使用相同的凭据来使用第三方交互式FTP程序执行相同的操作。