Hi,
In my web application, i need to show 'Progress Bar' of uploading status. I got the source from one of the CP member's post.
Link
Problem is,
I'm using master page and Content page for my whole application. While i try to upload from an simple aspx page it works fine. But while i tried using Master and Content its not works well.
My code structure is,
One of my content page contains <Frame> for loading a simple aspx page
like,
<iframe id="uploadFrame" frameborder="0" scrolling="yes" src="UploadEngine.aspx"> </iframe>
where "UploadEngine.aspx" Contains Code to Save a Particular File on Server.
Here we have an Class file named "UploadDetail.cs" contains four different variables used to maintain in Session for obtaining current upload status from "UploadEngine.aspx"
Hence while i click upload button from a Content page which contains <Iframe>. It just calls the "UploadEngine.aspx" and starts upload the files into my server's specified folder, but i can't getting the session variables updated for showing progress status. Its always remains null.
Hope you understand !
Please help me out...
My Upload.aspx
//Start upload process
function onUploadClick() {
if (fileUpload.value.length > 0) {
if (true) {
updateMessage(MessageStatus.Information, 'Initializing upload ...', '', '0 of 0 Bytes');
form.submit();
Sys.UI.DomElement.addCssClass($get('dvUploader'), 'StartUpload');
setProgress(0);
startProgress();
intervalID = window.setInterval(function() {
PageMethods.GetUploadStatus(function(result) {
if (result) {
setProgress(result.percentComplete);
updateMessage(MessageStatus.Information, result.message, result.fileName, result.downloadBytes);
if (result == 100) {
window.clearInterval(intervalID);
clearTimeout(subintervalID);
}
}
});
}, 500);
}
else
onComplete(MessageStatus.Error, "File name '" + filename + "' already exists in the list.", '', '0 of 0 Bytes');
}
else
onComplete(MessageStatus.Warning, 'You need to select a file.', '', '0 of 0 Bytes');
}
My Upload.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Session["UploadDetail"] = new UploadDetail { IsReady = false };
}
}
[System.Web.Services.WebMethod]
[System.Web.Script.Services.ScriptMethod]
public static object GetUploadStatus()
{
UploadDetail info = (UploadDetail)HttpContext.Current.Session["UploadDetail"];
if (info != null && info.IsReady)
{
long soFar = info.UploadedLength;
long total = info.ContentLength;
int percentComplete = (int)Math.Ceiling((double)soFar / (double)total * 100);
string message = "Uploading...";
string fileName = string.Format("{0}", info.FileName);
string downloadBytes = string.Format("{0} of {1} Bytes", soFar, total);
return new
{
percentComplete = percentComplete,
message = message,
fileName = fileName,
downloadBytes = downloadBytes
};
}
return null;
}
UploadDetail.cs
public class UploadDetail
{
public bool IsReady { get; set; }
public long ContentLength { get; set; }
public long UploadedLength { get; set; }
public long TotalUploadedLength { get; set; }
public string FileName { get; set; }
}
UploadEngine.aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
if (this.IsPostBack)
{
UploadDetail Upload = (UploadDetail)this.Session["UploadDetail"];
Upload.IsReady = false;
HttpFileCollection hfc = Request.Files;
string fileName = "";
int ContentTotalLen = 0;
for (int k = 0; k < hfc.Count; k++)
{
HttpPostedFile hhpf = hfc[k];
ContentTotalLen += hhpf.ContentLength;
}
if (ContentTotalLen > 0)
{
string path = @"E:\FTPFILES\";
Upload.IsReady = true;
Upload.TotalUploadedLength = 0;
for (int i = 0; i < hfc.Count; i++)
{
HttpPostedFile hpf = hfc[i];
fileName = System.IO.Path.GetFileName(hpf.FileName);
Upload.ContentLength = hfc[i].ContentLength;
Upload.UploadedLength = 0;
if (fileName != "")
{
Upload.FileName = fileName;
int bufferSize = 1;
byte[] buffer = new byte[bufferSize];
using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
while (Upload.UploadedLength < Upload.ContentLength)
{
int bytes = hfc[i].InputStream.Read(buffer, 0, bufferSize);
fs.Write(buffer, 0, bytes);
Upload.UploadedLength += bytes;
Upload.TotalUploadedLength += bytes;
}
}
}
}
const string js = "window.parent.onComplete(1,'File uploaded successfully.','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", string.Format(js, fileName, Upload.TotalUploadedLength, ContentTotalLen), true);
}
else
{
const string js = "window.parent.onComplete(4, 'There was a problem with the file.','','0 of 0 Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(UploadEngine), "progress", js, true);
}
}
}