繁体   English   中英

我想在C#应用程序中显示文件上传进度

[英]I want to show file upload progress in c# application

我想在C#Windows应用程序中显示文件上传进度因为我正在使用在线数据库,所以保存或上传需要更多时间。我正在使用以下代码。我已经搜索过,但未显示确切的进度百分比。想要在移动到另一个位置之前更改文件名。 提前致谢。

        OpenFileDialog dlg = new OpenFileDialog();

        DialogResult dlgRes = dlg.ShowDialog();


        Application.DoEvents();
        label14.Visible = true;
        if (DialogResult.OK != DialogResult.Cancel)
        {

            foreach (string file in dlg.FileNames)
            {
                try
                {
                    newpath = Path.Combine(textBox2.Text, Path.GetFileName(file)).ToString();
                    filenametemp = "tush" + Path.GetFileName(file).ToString();
                    if (String.IsNullOrEmpty(textBox2.Text))
                    {
                        MessageBox.Show("please select folder to save");
                    }
                    else
                    {
                        File.Copy(file, Path.Combine(textBox2.Text, Path.GetFileName(file)).ToString());


                        if (dlgRes != DialogResult.Cancel)
                        {
                            //Provide file path in txtFilePath text box.
                            txtFilePath.Text = dlg.FileName;
                        }
                        //string folderpath = System.IO.Directory.GetParent(Environment.CurrentDirectory).Parent.FullName + textBox2.Text;
                        //string filePath = textBox2.Text + System.IO.Path.GetFileName(dlg.FileName);
                        //System.IO.File.Copy(dlg.FileName, folderpath, true);

                        try
                        {
                            //Read File Bytes into a byte array
                            byte[] FileData = ReadFile(txtFilePath.Text);

                            //Initialize SQL Server Connection
                            SqlConnection CN = new SqlConnection(ConfigurationManager.ConnectionStrings["Copymanagment"].ConnectionString);

                            //Set insert query
                            string qry = "insert into fileinfo (File_Id,File_Path,date,OriginalPath,Billnumber,Billdate,FileData) values(@FileId,@Filepath,@Date,@OriginalPath,@Billnumber,@Billdate,@FileData)";

                            //Initialize SqlCommand object for insert.
                            SqlCommand SqlCom = new SqlCommand(qry, CN);




                            string path = textBox2.Text;
                            string[] arrpath = null;
                            int count;
                            arrpath = path.Split('\\');
                            for (count = 0; count <= arrpath.Length - 1; count++)
                            {
                                // MessageBox.Show(arrpath[count]);
                            }
                            string folder = arrpath[arrpath.Length - 1] + databaseid().ToString();

                            //We are passing Original File Path and File byte data as sql parameters.
                            string fileid = Path.GetDirectoryName(dlg.FileName);
                            SqlCom.Parameters.Add(new SqlParameter("@FileId", (object)folder));
                            SqlCom.Parameters.Add(new SqlParameter("@Filepath", (object)newpath));
                            SqlCom.Parameters.Add(new SqlParameter("@Date", (object)System.DateTime.Now.ToString("dd-MM-yyyy hh:mm:ss tt")));
                            SqlCom.Parameters.Add(new SqlParameter("@OriginalPath", (object)txtFilePath.Text));
                            SqlCom.Parameters.Add(new SqlParameter("@Billnumber", (object)textbillno.Text));
                            SqlCom.Parameters.Add(new SqlParameter("@Billdate", (object)dateTimePicker1.Value.ToString("dd-MM-yyyy")));
                            SqlCom.Parameters.Add(new SqlParameter("@FileData", (object)FileData));

                            //Open connection and execute insert query.
                            CN.Open();
                            SqlCom.ExecuteNonQuery();
                            CN.Close();
                           label14.Visible = false;
                            //Close form and return to list or Files.
                            MessageBox.Show("File saved Succsesfully");
                            txtFilePath.Text = "";

                        }
                        catch (Exception ex)
                        {
                            MessageBox.Show(ex.ToString());
                        }

                    }
                }
                catch
                {
                    MessageBox.Show("File already available");
                }
            }
        }       

完成此操作的方式是,将一条完整的数据插入到一个快照中并阻塞直到执行一次,而没有机会更新tge UI的一条语句。 您可以使用UPDATETEXT在块中上传BLOB数据。 有关如何执行此操作的完整说明,请参见https://msdn.microsoft.com/zh-cn/library/vstudio/3517w44b(v=vs.100).aspx 这样做,您将有机会更新prigress bar的值。

您可以使用asp.net ajax控件工具包。 有一个ajax文件上传控件。

注意不要与同一个库中的asynFile上传控件混淆(该控件没有进度条)。

您可以在此处获取信息http://www.codingfusion.com/Post/AjaxFileUpload-example-to-upload-multiple-files-in

要下载该库,请访问此处http://www.codingfusion.com/Post/3-Different-ways-to-add-AjaxControlToolkit-in-Asp

对于Windows应用,请尝试遵循https://stackoverflow.com/a/9446524/4810628

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM