当我尝试在FTP服务器中发送文件时遇到问题,只有在使用安全SSL连接时才会出现此问题。在上传结束时,服务器发送错误代码“426数据连接意外关闭,上传中止”。当我使用未加密(PlainSocket)连接时,上传正确完成。
感谢您的帮助,您有任何想法如何解决这个问题吗?
以下是一些代码:
//reading selected files from file picker
var picker = new Windows.Storage.Pickers.FileOpenPicker();
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail;
picker.SuggestedStartLocation =
Windows.Storage.Pickers.PickerLocationId.DocumentsLibrary;
picker.FileTypeFilter.Add("*");
var files = await picker.PickMultipleFilesAsync();
if (files != null && files.Count > 0)
{
TxtB_ProgressTitle.Text = "Uploading...";
Grid_Progress.Visibility = Windows.UI.Xaml.Visibility.Visible;
App.FtpClient.Progressing -= FtpClient_Progressing;
App.FtpClient.Progressing += FtpClient_Progressing;
byte[] data = null;
foreach (var file in files)
{
using (var reader = await file.OpenStreamForReadAsync())
{
data = new byte[reader.Length];
int read = await reader.ReadAsync(data, 0, (int)reader.Length);
}
InitProgress((int)(data.Length / 1024), file.Name);
try
{
await vm.UploadFile(data, file.Name);
}
catch (FtpPermissionDeniedException exc)
{
MessageDialog dlg = new MessageDialog("Permission denied", "Error uploading \"" + file.Name + "\" file");
await dlg.ShowAsync();
}
catch (FtpUploadException exc)
{
MessageDialog dlg = new MessageDialog("Data connection unexpectedly closed, upload aborted", "Error uploading \"" + file.Name + "\" file");
await dlg.ShowAsync();
}
catch (FtpException exc)
{
MessageDialog dlg = new MessageDialog(exc.Message, "Error uploading \"" + file.Name + "\" file");
await dlg.ShowAsync();
}
}
上传ftp文件
public async Task<bool> Upload(string name, byte[] data)
{
var channel = await OpenChannelForDataTransfert();
FtpResponse response = await FtpCommand.Execute(FtpTrace, FtpCommandType.UploadFile, name);
if (response.Code == 150 || response.Code == 125)
{
channel.Progress += (sender, args) => OnProgress(args);
bool dataUploaded = await channel.Upload(data);
if (dataUploaded)
{
channel.Dispose();
response = await FtpCommand.ExecuteRequest(null, FtpTrace);
if (response.Code == 226)
{
return true;
}
else if (response.Code == 550)
{
throw new FtpPermissionDeniedException();
}
else if (response.Code == 426)
{
throw new FtpUploadException();
}
}
}
return false;
}
开放转移渠道:
private async Task<FtpChannel> OpenChannelForDataTransfert()
{
FtpResponse res = null;
res = await FtpCommand.Execute(FtpTrace, FtpCommandType.BinaryMode);
if (res.Code == 200)
{
res = await FtpCommand.Execute(FtpTrace, FtpCommandType.Passive);
if (res.Code == 227)
{
var host = FtpParser.ParseFtpHostName(res.Message, FtpTrace);
if (host != null)
{
var channel = new FtpChannel(this, host, IsSecure);
if ((await channel.ConnectAsync()))
{
return channel;
}
}
}
}
return null;
}
上传文件
public async Task<bool> Upload(byte[] data)
{
FtpProgressEventArgs args = new FtpProgressEventArgs();
_TotalSize = data.Length / 1024;
_CurrentSize = 0;
args.TotalSize = _TotalSize;
using (var stream = _Output.AsStreamForWrite())
{
using (MemoryStream d = new MemoryStream(data))
{
int read = 0;
byte[] buffer = new byte[_BufferSize];
while ((read = await d.ReadAsync(buffer, 0, buffer.Length)) != 0)
{
await stream.WriteAsync(buffer, 0, read);
_CurrentSize += read;
args.CurrentSize = CurrentSize;
OnProgress(args);
}
stream.Flush();
}
}
return true;
}
FtpTrace:
[[ExecuteRequest] Command] USER : ********
[[ParseFtpCommand]] 331 User name okay, need password. :
[[ExecuteRequest] Command] PASS : *************
[[ParseFtpCommand]] 230 User logged in, proceed. :
[[ExecuteRequest] Command] PBSZ 0 :
[[ParseFtpCommand]] 200 PBSZ command OK. Protection buffer size set to 0. :
[[ExecuteRequest] Command] PROT P :
[[ParseFtpCommand]] 200 PROT command OK. Using private data connection. :
[[ExecuteRequest] Command] OPTS UTF8 ON :
[[ParseFtpCommand]] 200 OPTS UTF8 is set to ON. :
[[ExecuteRequest] Command] PWD :
[[ParseFtpCommand]] 257 "/" is current directory. :
[[ExecuteRequest] Command] TYPE I :
[[ParseFtpCommand]] 200 Type set to I. :
[[ExecuteRequest] Command] PASV :
[[ParseFtpCommand]] 227 Entering Passive Mode (**,**,**,**,156,103) :
[[ExecuteRequest] Command] MLSD :
[[ParseFtpCommand]] 150 Opening BINARY mode data connection for MLSD. :
[[ParseFtpCommand]] 226 Transfer complete. 544 bytes transferred. 0.53 KB/sec. :
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
[[ParseFtpFile]] : FooBar File
Le thread 0x15c8s'estrrêtéavecle code 0(0x0)。
[[ExecuteRequest] Command] TYPE I :
[[ParseFtpCommand]] 200 Type set to I. :
[[ExecuteRequest] Command] PASV :
[[ParseFtpCommand]] 227 Entering Passive Mode (**,**,**,**,156,106) :
[[ExecuteRequest] Command] STOR : HomeBanner.png
[[ParseFtpCommand]] 150 Opening BINARY mode data connection for HomeBanner.png. :
[[ParseFtpCommand]] 426 Data connection unexpectedly closed, receive file /HomeBanner.png aborted. :
谢谢你们