.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> File Operation >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

File Upload with AJAX.NET Progress Bar

Posted By:Athira Sreejith       Posted Date: December 31, 2008    Points: 10    Category: File Operation    URL: http://www.dotnetspark.com  
 

File Upload with AJAX.NET Progress Bar. This uses third party library from http://www.expandata.net/ and Its free.

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using expandata.net;

public partial class samples_CS_uploadsamples_Advanced_UploadWithAjaxProgress_uploadwithAjaxNETprogress : System.Web.UI.Page
{
protected string ProgID = "";
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
GenerateProgressID();
Session["PROGRESSID"] = ProgressID;
Session.Remove("CANCELLED");
}
}

private void GenerateProgressID()
{
ProgressID = (new DNUpFilesBE()).NextProgressID();
}

protected void ButtonUpload_Click(object sender, System.EventArgs e)
{
this.ButtonUpload.Enabled = false; ProcessUpLoad();
}
void ProcessUpLoad()
{
expandata.net.DNUpFilesBE dnUpFilesBE = new expandata.net.DNUpFilesBE();
expandata.net.DNFileBE dnFileBE;
try
{ dnUpFilesBE.CacheFolder = Server.MapPath((string)Application["vroot"]) + "/Cache";
dnUpFilesBE.DestinationFolder = Server.MapPath((string)Application["vroot"]) + "/Dest";
dnUpFilesBE.ProcessRequest(this.Request);
if (dnUpFilesBE.Files.Count > 0)
LabelTitle.Text = "
All files have been successfully processed by UpFilesBE!!";
else
LabelTitle.Text = "
No files have been submitted to UpFilesBE!!";
LabelSaveInfo.Text = "";
end
if System.Text.StringBuilder saveInfo = new System.Text.StringBuilder();
for (int i = 0; i < dnUpFilesBE.Files.Count; i++)
{ dnFileBE = dnUpFilesBE.Files[i];
if (i != 0) { saveInfo.Append("
");
saveInfo.Append("
----------------------------------------");
}
saveInfo.Append("
File Number (" + ((i + 1).ToString()) + ")" + "");
saveInfo.Append("
");
if (dnFileBE.Size == 0)
{
LabelTitle.Text = "
Some files could not be processed by UpFilesBE!!";
saveInfo.Append("
Please enter a valid filename to upload;");
}
else if (dnFileBE.Size > 0)
{
dnFileBE.Overwrite = true;
dnFileBE.Save();
switch (dnFileBE.Processed)
{
case dnSaveResultsEnum.dnSaved: saveInfo.Append("
File name::
" + dnFileBE.Name + "");
saveInfo.Append("
Form File Field name::
" + dnFileBE.FormName + "");
saveInfo.Append("
File Save result of type dnSaveResultsEnum::
" + dnFileBE.Processed + "");
saveInfo.Append("
Full path of the file as it appears on the client::
" + dnFileBE.ClientPath + "");
saveInfo.Append("
Full path of the file as saved in the destination folder on the server::
" + dnFileBE.DestinationPath + "");
saveInfo.Append("
Size of the file in Bytes ::
" + dnFileBE.Size + " bytes");
saveInfo.Append("
File Mime Type::
" + dnFileBE.ContentType + "");
break;
case dnSaveResultsEnum.dnError: LabelTitle.Text = "Files could not be saved by UpFilesBE!!";
saveInfo.Append("
" + dnFileBE.Error + "");
break;
default: LabelTitle.Text = "Files could not be saved by UpFilesBE!!";
saveInfo.Append("An unknown error has occurred. dnSaveResultsEnum: " + dnFileBE.Processed + ". " + dnFileBE.Error);
break;
}
}
}
LabelSaveInfo.Text = saveInfo.ToString();
}
catch (Exception ex)
{
Page.Response.Write("WebServer exception: " + ex.Message);
}
finally
{
if (dnUpFilesBE != null)
{
dnUpFilesBE.Dispose();
dnUpFilesBE = null;
}
}
}

protected void Timer1_Tick(object sender, EventArgs e)
{
if (Session["PROGRESSID"] == null)
return; ProgressID = Session["PROGRESSID"].ToString();
string[] Progressparams = GetProgress(ProgressID);
string temp = Progressparams[0]; if (temp == "0%")
{
this.LabelStatus.Text = "Waiting...";
if (Session["CANCELLED"] != null)
{
this.LabelStatus.Text = "Cancelling...";
} ButtonCancel.Enabled = false; return;
}
this.LabelStatus.Text = "Transfering...";
ButtonCancel.Enabled = true;
temp = temp.Substring(0, temp.Length - 1);
int width = System.Convert.ToInt32(temp);
TablePercentage.Width = Unit.Percentage(width);
this.LabelDataUploaded.Text = temp + "%" + " (" + Progressparams[9] + ")/";
this.LabelDataUploaded.Text += "(" + Progressparams[8] + ") Uploaded at ";
this.LabelDataUploaded.Text += Progressparams[2];
this.LabelCurrentFile.Text = Progressparams[1];
this.LabelTimeElapsed.Text = Progressparams[5];
this.LabelTimeRemaining.Text = Progressparams[6];
}

public string[] GetProgress(string pID)
{
string[] Progressparams = new string[10];
DNUpFilesProgressBE dNUpFilesProgressBE = new DNUpFilesProgressBE();
dNUpFilesProgressBE.ProgressId = pID;
dNUpFilesProgressBE.Monitor = true;
int per = dNUpFilesProgressBE.Percentage;
string temp = (System.Web.UI.WebControls.Unit.Percentage(per)).ToString();
Progressparams[0] = temp;
temp = dNUpFilesProgressBE.CurrentFile;
Progressparams[1] = temp;
temp = (dNUpFilesProgressBE.BytesPerSecondAverage / 1024).ToString() + " KB/sec"; Progressparams[2] = temp;
temp = dNUpFilesProgressBE.Finished.ToString(); Progressparams[3] = temp; temp = dNUpFilesProgressBE.Percentage.ToString() + "%";
Progressparams[4] = temp; temp = dNUpFilesProgressBE.TimeElapsed;
Progressparams[5] = temp; temp = dNUpFilesProgressBE.TimeRemaining;
Progressparams[6] = temp; temp = dNUpFilesProgressBE.Started.ToString();
Progressparams[7] = temp; temp = ((float)dNUpFilesProgressBE.TotalBytes / 1024).ToString() + " KB";
Progressparams[8] = temp; temp = ((float)dNUpFilesProgressBE.TransferredBytes / 1024).ToString() + " KB";
Progressparams[9] = temp; return Progressparams;
}

protected void ButtonCancel_Click(object sender, EventArgs e)
{
if (Session["PROGRESSID"] == null) return;
ProgressID = Session["PROGRESSID"].ToString();
CancelUpload(ProgressID); this.LabelStatus.Text = "Canceling...";
Session["CANCELLED"] = "True";
}
public int CancelUpload(string ProgressID)
{
DNUpFilesProgressBE dNUpFilesProgressBE = new DNUpFilesProgressBE();
dNUpFilesProgressBE.ProgressId = ProgressID;
dNUpFilesProgressBE.Cancel();
return 0;
}
}


     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend