问题描述
- winfromFTP大文件上传问题。急!
-
winfrom使用FTP进行大文件上传时,没有加入线程的时候,上传上去,服务器直接把数据写入到文件,是可以断点续传的,但是用户界面假死;加入线程或者backgroundwork之后,用户界面不会假死,但是上传上去的文件没有及时的写入,关闭程序之后,服务器上面的文件什么都没有写入,还把之前的删掉了。请问有没有方法可以解决这个问题?急!!
解决方案
不知道你是怎么加入线程的。以及你是怎么上传的。该同步的有没有同步。
解决方案二:
我现在用的是backgroundwork.界面上有个上传按钮,点击按钮后开始这个方法。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (!client.Connected)
client.Connect();
string[] dirs = client.Dir(remotingFolder + "");
client.ChDir(remotingFolder); //改变目录
if (this.textBox1.Text != "")
{
bool flag = true;
foreach (string dir in dirs)
{
string[] infos = dir.Split(' ');
string info = infos[infos.Length - 1].Replace("r", "");
FileInfo fileInfo = new FileInfo(this.textBox1.Text);
string localfile = fileInfo.Name;
if (info == localfile)//断点续传
{
flag = false;
long remotingSize = client.GetFileSize(info);
long localSize = fileInfo.Length;
int i = (int)(remotingSize * 100 / localSize);
this.backgroundWorker1.ReportProgress(i,0);
if (remotingSize != localSize)
{
client.PutBrokenFile(info, remotingFolder, this.textBox1.Text, remotingSize);
}
}
}
if (flag)
client.Put(this.textBox1.Text);
}
}
解决方案三:
我现在用的是backgroundwork.界面上有个上传按钮,点击按钮后开始这个方法。
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (!client.Connected)
client.Connect();
string[] dirs = client.Dir(remotingFolder + "");
client.ChDir(remotingFolder); //改变目录
if (this.textBox1.Text != "")
{
bool flag = true;
foreach (string dir in dirs)
{
string[] infos = dir.Split(' ');
string info = infos[infos.Length - 1].Replace("r", "");
FileInfo fileInfo = new FileInfo(this.textBox1.Text);
string localfile = fileInfo.Name;
if (info == localfile)//断点续传
{
flag = false;
long remotingSize = client.GetFileSize(info);
long localSize = fileInfo.Length;
int i = (int)(remotingSize * 100 / localSize);
this.backgroundWorker1.ReportProgress(i,0);
if (remotingSize != localSize)
{
client.PutBrokenFile(info, remotingFolder, this.textBox1.Text, remotingSize);
}
}
}
if (flag)
client.Put(this.textBox1.Text);
}
}
解决方案四:
上传代码如下:
///
/// 上传一批文件
///
/// 本地目录(不得以结束)
/// 文件名匹配字符(可以包含*和?)
public void Put(string strFolder, string strFileNameMask)
{
string[] strFiles = Directory.GetFiles(strFolder, strFileNameMask);
foreach (string strFile in strFiles)
{
//strFile是完整的文件名(包含路径)
Put(strFile);
}
}
/// <summary>
/// 上传一个文件
/// </summary>
/// <param name="strFileName">本地文件名</param>
public void Put(string strFileName)
{
if (!bConnected)
{
Connect();
}
Socket socketData = CreateDataSocket();
SendCommand("STOU " + Path.GetFileName(strFileName));
long size = new FileInfo(strFileName).Length;
if (!(iReplyCode == 125 || iReplyCode == 150))
{
throw new IOException(strReply.Substring(4));
}
FileStream input = new
FileStream(strFileName, FileMode.Open);
int iBytes = 0;
long current = 0;
while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
{
int i = socketData.Send(buffer, iBytes, 0);
current += i;
keepProgress(current, size);
}
input.Close();
if (socketData.Connected)
{
socketData.Close();
}
if (!(iReplyCode == 226 || iReplyCode == 250))
{
ReadReply();
if (!(iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
}
}
/// <summary>
/// 断点上传
/// </summary>
/// <param name="strRemoteFileName">要上传的文件名</param>
/// <param name="strFolder">本地目录(不得以结束)</param>
/// <param name="strLocalFileName">保存在本地时的文件名</param>
public void PutBrokenFile(string strRemoteFileName, string strFolder, string strLocalFileName, long size)
{
if (!bConnected)
{
Connect();
}
SetTransferType(TransferType.Binary);
Socket socketData = CreateDataSocket();
SendCommand("APPE " + Path.GetFileName(strLocalFileName));
long localsize = new FileInfo(strLocalFileName).Length;
if (!(iReplyCode == 125 || iReplyCode == 150))
{
throw new IOException(strReply.Substring(4));
}
FileStream input = new
FileStream(strLocalFileName, FileMode.Open);
int iBytes = 0;
input.Seek(size, SeekOrigin.Current);
long current = size;
while ((iBytes = input.Read(buffer, 0, buffer.Length)) > 0)
{
current += iBytes;
socketData.Send(buffer, iBytes, 0);
keepProgress(current, localsize);
}
input.Close();
if (socketData.Connected)
{
socketData.Close();
}
if (!(iReplyCode == 226 || iReplyCode == 250))
{
ReadReply();
if (!(iReplyCode == 226 || iReplyCode == 250))
{
throw new IOException(strReply.Substring(4));
}
}
}
解决方案五:
已经解决了,socket非正常关闭。数据就会丢失,正常关闭,数据就能保存。那么问题又来了,如果传输过程中网络中断。socket非正常关闭。那么之前的数据怎么保存?