状況情報のプログレスバーを含む複数のファイルアップロード用の小さなスクリプトを作成しました。
アップロードのために選択したすべてのファイルに関するpogressbarの値を計算する方法が不思議です。
ファイルのアップロードが以下のように初期化されるのBackgroundWorkerで動作します:複数ファイルアップロードのプログレスバー値を計算する
public Form()
{
worker = new BackgroundWorker();
worker.WorkerReportsProgress = true;
worker.WorkerSupportsCancellation = true;
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
}
ボタンクリックでファイルダイアログを開き、BackgroundWorkerのは、その作業を開始:BackgroundWorkerのは、その作業を始めた
private void button1_Click(object sender, EventArgs e)
{
if (worker.IsBusy)
{
worker.CancelAsync();
}
else
{
OpenFileDialog od = new OpenFileDialog();
od.Multiselect = true;
if (od.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
if (progressBar1.Value == progressBar1.Maximum)
{
progressBar1.Value = progressBar1.Minimum;
}
string ftpServerIP = textBox1.Text;
string ftpUserID = textBox2.Text;
string ftpPassword = textBox3.Text;
List<object> arguments = new List<object>();
arguments.Add(progressBar1.Value);
arguments.Add(od.FileNames);
arguments.Add(ftpServerIP);
arguments.Add(ftpUserID);
arguments.Add(ftpPassword);
worker.RunWorkerAsync(arguments);
}
}
}
をFTP接続が確立され、FileOpenDialogからすべてのファイルが転送されます。次のコードの最後にファイルを満たすように変更する必要があります計算プログレスバーのための私のサンプルは、全サイズである:
private void worker_DoWork(object sender, DoWorkEventArgs e)
{
List<object> arguments = e.Argument as List<object>;
int percentFinished = (int)arguments[0];
string[] fileNames = arguments[1] as string[];
string ftpServerIP = arguments[2] as string;
string ftpUserID = arguments[3] as string;
string ftpPassword = arguments[4] as string;
while (!worker.CancellationPending && percentFinished < 100)
{
foreach (string fileName in fileNames)
{
FileInfo fileInf = new FileInfo(fileName);
// Create FtpWebRequest object from the Uri provided
reqFTP = (FtpWebRequest)FtpWebRequest.Create(new Uri("ftp://" + ftpServerIP + "/" + fileInf.Name));
// FTP Credentials
reqFTP.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
// By default KeepAlive is true, where the control connection is not closed after a command is executed.
reqFTP.KeepAlive = false;
// Specify the command to be executed.
reqFTP.Method = WebRequestMethods.Ftp.UploadFile;
// Specify the data transfer type.
reqFTP.UseBinary = true;
// Notify the server about the size of the uploaded file
reqFTP.ContentLength = fileInf.Length;
// The buffer size is set to 2kb
int buffLength = 2048;
byte[] buff = new byte[buffLength];
int contentLen;
// Opens a file stream (System.IO.FileStream) to read the file to be uploaded
FileStream fs = fileInf.OpenRead();
try
{
// Stream to which the file to be upload is written
Stream strm = reqFTP.GetRequestStream();
// Read from the file stream 2kb at a time
contentLen = fs.Read(buff, 0, buffLength);
// Until Stream content ends
while (contentLen != 0)
{
// Write Content from the file stream to the FTP Upload Stream
strm.Write(buff, 0, contentLen);
contentLen = fs.Read(buff, 0, buffLength);
}
// Close the file stream and the Request Stream
strm.Close();
fs.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Upload Error");
}
}
// ToDO: Calculate progressBar
percentFinished++;
worker.ReportProgress(percentFinished);
System.Threading.Thread.Sleep(50);
}
e.Result = percentFinished;
}
私はタイムアウトを削除する必要がありましたSOLUTION
と作業者は正しい値を報告します。
percentFinished++;
worker.ReportProgress(percentFinished);
//System.Threading.Thread.Sleep(50);
ここで、percentFinishedには注意してください。プログレスバーにパーセント値が渡されます。それが100%で終わると終了します。 progressBar1.Maximumは常に100です。fileNames.Lengthとpercentageを考慮して進捗状況を計算するにはどうすればよいですか? – njlqay
これは私の言うところですが、コードに「要素の合計」と「要素のマップ」があるので、別の変数などを使用する必要はありません。プログレスバーの最大値は100でなくてもよく、配列の要素数に最大値を設定するだけで、アップロードを完了するたびに値を1ずつ増やすことができます。あなたのロジックが間違っていますか?さらに、パーセント値を数値として計算したいのであれば、 'int percResult = mappedElements * 100/totalElements;となるでしょう。 –
それはポイントです。サイズが50 MBのファイルが1つしかないときは、あなたのロジックを使用して、50 MBがアップロードされてから1に切り替わるまでプログレスバーがゼロになるようにしてください。私の意図は、どのファイルサイズがすでにアップロードされているかを認識することです。 20 MBの50 MBの1つのファイルと40%のprogressBarにアップロードされた表示... – njlqay