我想在c#windows应用程序中显示文件上传进度因为我使用在线数据库需要更多时间来保存或上传。我正在使用以下Code.I已搜索但它没有显示确切的百分比明智进度。我想在转移到另一个地方之前更改文件名。 在此先感谢。
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");
}
}
}
答案 0 :(得分:0)
你完成它的方式,一次性插入整个数据并阻塞直到完成没有机会更新tge UI。您可以使用UPDATETEXT在chuncks中上传BLOB数据。有关如何执行此操作的完整说明,请参阅https://msdn.microsoft.com/en-us/library/vstudio/3517w44b(v=vs.100).aspx。这样做,您将有机会更新prigress栏的值。
答案 1 :(得分:-1)
您可以使用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