FTP上传大文件 - 无限上传过程 [英] FTP Uploading Large Files - Infinite Upload Process

查看:93
本文介绍了FTP上传大文件 - 无限上传过程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好!



我正在开发大型文件上传的FTP流程(大小> 50MB)。我有一个线程进程将文件上传到服务器,但文件继续增长,直到我杀死整个Windows应用程序。如果我不杀死这个过程,它将继续永远增长。



这就是我所说的:



  if (ofd_psUpdate.ShowDialog()== DialogResult.OK)
{
Thread thread = new Thread(UploadFile);
thread.Start();
}

private void UploadFile( object obj)
{
try
{
uploadPicoScopeButton.Invoke
((MethodInvoker)(()= > {uploadPicoScopeButton.BackColor = Color.Lime;}));

FtpWebRequest request =(FtpWebRequest)FtpWebRequest.Create( ftp://www.pico -usa.com / + ofd_psUpdate.SafeFileName);
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Credentials = new NetworkCredential(ftpUserNameTextBox.Text.Trim(),ftpPasswordTextBox.Text.Trim());
request.UsePassive = true ;
request.UseBinary = false ;
request.KeepAlive = true ;

stream = File.OpenRead(ofd_psUpdate.FileName);
byte [] buffer = new byte [stream.Length];
int bytesSize = 0 ;

reqStream = request.GetRequestStream();

while ((bytesSize = stream.Read(buffer, 0 ,缓冲区.Length))> 0
{
reqStream.Write(buffer , 0 ,bytesSize);
this .Invoke( new UpdateProgessCallback( this .UpdateProgress), new object [] {bytesSize,stream.Length});
}

uploadPicoScopeButton.Invoke
((MethodInvoker)(()= > {uploadPicoScopeButton.BackColor = Color 。透明; }));
}
catch (例外情况)
{
uploadPicoScopeButton.Invoke
((MethodInvoker)(( )= > {uploadPicoScopeButton.BackColor = Color.Red;}));

MessageBox.Show( FTP上传失败\ nn \ n + ex.Message);

uploadPicoScopeButton.Invoke
((MethodInvoker)(()= > {uploadPicoScopeButton.BackColor = Color.Transparent;}) );
}
}

私有 void UpdateProgress( Int64 BytesRead, Int64 TotalBytes)
{
progressLabel.BeginInvoke(
(MethodInvoker)委托(){progressLabel.Text = 上传 + BytesRead + + TotalBytes + + PercentProgress + %);
progressLabel.Refresh();});
}





有人能说出我是否遗漏了一些简单的东西,以防止文件上传过程成为一个无限的过程?我的目标是当上传的文件数量等于文件的实际大小时,停止进程,不多也不少。



谢谢大家!

解决方案

我可能会改变这个



  byte  [] buffer =  new   byte  [stream.Length ]。 
int bytesSize = 0 ;

reqStream = request.GetRequestStream();

while ((bytesSize = stream.Read(buffer, 0 ,缓冲区.Length))> 0
{
reqStream.Write(buffer , 0 ,bytesSize);
this .Invoke( new UpdateProgessCallback( this .UpdateProgress), new object [] {bytesSize,stream.Length});
}





to



  byte  [] buffer =  new   byte  [ 1024 ]; 
int bytesSize = 0 ;

reqStream = request.GetRequestStream();

do
{
bytesSize = stream.Read(buffer, 0 ,buffer.Length)
reqStream.Write(buffer, 0 ,bytesSize);
this .Invoke( new UpdateProgressCallback( this .UpdateProgress), new object [] {bytesSize,stream.Length});
}
while (bytesSize!= 0 );


Hi everyone!

I am in the process of developing an FTP process for large file uploads (>50MB in size). I have a threading process that uploads the file to the server, but the file continues to grow until I kill the whole windows application. If I don't kill the process, it will continue to grow forever.

Here's what I'm referring to:

if (ofd_psUpdate.ShowDialog() == DialogResult.OK)
{
     Thread thread = new Thread(UploadFile);
     thread.Start();
}

private void UploadFile(object obj)
{
     try
     {
          uploadPicoScopeButton.Invoke
               ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Lime; } ));

          FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create("ftp://www.pico-usa.com/" + ofd_psUpdate.SafeFileName);
          request.Method = WebRequestMethods.Ftp.UploadFile;
          request.Credentials = new NetworkCredential(ftpUserNameTextBox.Text.Trim(), ftpPasswordTextBox.Text.Trim());
          request.UsePassive = true;
          request.UseBinary = false;
          request.KeepAlive = true;

          stream = File.OpenRead(ofd_psUpdate.FileName);
          byte[] buffer = new byte[stream.Length];
          int bytesSize = 0;

          reqStream = request.GetRequestStream();

          while ((bytesSize = stream.Read(buffer, 0, buffer.Length)) > 0)
          {
               reqStream.Write(buffer, 0, bytesSize);
               this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { bytesSize, stream.Length });
          }
        
          uploadPicoScopeButton.Invoke
               ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Transparent; }));
          }
          catch (Exception ex)
          {
               uploadPicoScopeButton.Invoke
                    ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Red; }));

               MessageBox.Show("FTP Upload Fail\n\n" + ex.Message);
                
               uploadPicoScopeButton.Invoke
                    ((MethodInvoker)(() => { uploadPicoScopeButton.BackColor = Color.Transparent; }));
          }
}

private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
{
     progressLabel.BeginInvoke(
     (MethodInvoker)delegate() { progressLabel.Text = "Uploaded " + BytesRead + " of " + TotalBytes + " (" + PercentProgress + "%)";
     progressLabel.Refresh(); });
}



Can anyone tell if I'm missing something simple to prevent the file upload process from being an infinite process? My goal is to have the process stop when the amount of the file that has been uploaded equals the actual size of the file, no more no less.

Thanks everyone!

解决方案

I probably would have changed this

byte[] buffer = new byte[stream.Length];
          int bytesSize = 0;

          reqStream = request.GetRequestStream();

          while ((bytesSize = stream.Read(buffer, 0, buffer.Length)) > 0)
          {
               reqStream.Write(buffer, 0, bytesSize);
               this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { bytesSize, stream.Length });
          }



to

byte[] buffer = new byte[1024];
          int bytesSize = 0;

          reqStream = request.GetRequestStream();
          
          do 
          {
               bytesSize = stream.Read(buffer,0,buffer.Length)
               reqStream.Write(buffer,0, bytesSize);
               this.Invoke(new UpdateProgressCallback(this.UpdateProgress),new object[] {bytesSize, stream.Length });
          } 
          while (bytesSize != 0);


这篇关于FTP上传大文件 - 无限上传过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆