Dear Code Project Expert Team,
I have big issue when we upload file for upload mechanism. And also want to Percentage completion done and display of completion process on front.
All of working fine file upload and complete successfully done.
But process bar not working on server. i retrieve completion % in session. But session not working in PageMethods on Live server. Also all of functionality working in LOCALHOST.
STEP : 1 Uploading File
if (((SessionBasket)(Session["SessionHandler"])) != null)
{
if (((((SessionBasket)(Session["SessionHandler"])).UserID) != null) || ((((SessionBasket)(Session["SessionHandler"])).UserID) != ""))
{
UploadDetail Upload = (UploadDetail)HttpContext.Current.Session["UploadDetail"];
#region "Post Back"
if (this.IsPostBack)
{
if (Upload != null)
{
if ((Upload.CourseId != null) && (Upload.CourseName != null))
{
Upload.IsReady = false;
if (this.fileUpload.PostedFile != null && this.fileUpload.PostedFile.ContentLength > 0)
{
string MainPath = this.Server.MapPath(@"../../UploadCourses");
if (!Directory.Exists(MainPath))
{
Directory.CreateDirectory(MainPath);
}
string subFolderName = (((SessionBasket)(Session["SessionHandler"])).AccName).ToString() + "_Courses";
if (!Directory.Exists(Path.Combine(MainPath, subFolderName)))
{
Directory.CreateDirectory(Path.Combine(MainPath, subFolderName));
}
string subFolderPath = MainPath + "\\" + subFolderName;
string subCourseFolderName = Upload.CourseName;
if (!Directory.Exists(Path.Combine(subFolderPath, subCourseFolderName)))
{
Directory.CreateDirectory(Path.Combine(subFolderPath, subCourseFolderName));
}
string path = this.Server.MapPath(@"../../UploadCourses/" + subFolderName + "\\" + subCourseFolderName);
string fileName = Path.GetFileName(this.fileUpload.PostedFile.FileName);
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(this.fileUpload.PostedFile.FileName);
string[] filePaths = Directory.GetFiles(path);
foreach (string filePath in filePaths)
File.Delete(filePath);
string[] delSubfolder = Directory.GetDirectories(path);
foreach (string FolderPath in delSubfolder)
{
DirectoryInfo deldirectory = new DirectoryInfo(FolderPath);
deldirectory.Delete(true);
}
Upload.ContentLength = this.fileUpload.PostedFile.ContentLength;
Upload.FileName = fileName;
Upload.UploadedLength = 0;
Upload.IsReady = true;
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
using (FileStream fs = new FileStream(Path.Combine(path, fileName), FileMode.Create))
{
while (Upload.UploadedLength < Upload.ContentLength)
{
int bytes = this.fileUpload.PostedFile.InputStream.Read(buffer, 0, bufferSize);
fs.Write(buffer, 0, bytes);
Upload.UploadedLength += bytes;
}
}
string strZipFolderName = ExtarctZipFile(path + "\\" + fileName, path);
string strFileVirtualPath = "\\UploadCourses\\" + subFolderName + "\\" + subCourseFolderName + "\\" + strZipFolderName;
string strImanifestPathRead = this.Server.MapPath(@"../../UploadCourses/" + subFolderName + "\\" + subCourseFolderName + "\\" + strZipFolderName);
if (Upload.ScormCheckId == 2)
{
int intManifest = File_Imsmanifest_Identify_Resources(strImanifestPathRead);
if (intManifest == 0)
{
string[] strDelSubfolder = Directory.GetDirectories(path);
foreach (string strFolderPath in strDelSubfolder)
{
DirectoryInfo dideldirectory = new DirectoryInfo(strFolderPath);
dideldirectory.Delete(true);
}
const string js = "window.parent.onComplete(4,'Course does not contain imanifest file.','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, fileName, Upload.UploadedLength, Upload.ContentLength), true);
}
if (intManifest == 1)
{
Utility objUtility = new Utility();
strimanifestfile = objUtility.ManifestRead(strImanifestPathRead + "\\imsmanifest.xml", "file", strImanifestPathRead);
}
}
#region "Scorm and non scorm file checking and database insert"
if ((Upload.ScormCheckId == 1) || (Upload.ScormCheckId == 2))
{
objCourseManagement = new CourseManagement();
objBAL_AUI_CourseManagement = new BAL_AUI_CourseManagement();
#region "Scorm Manifest file checking"
if (Upload.ScormCheckId == 2)
{
if ((strimanifestfile == "Mapping Right"))
{
objCourseManagement.CourseId = Upload.CourseId;
objCourseManagement.FileName = fileName.Trim();
objCourseManagement.DocumentLink = strFileVirtualPath;
objCourseManagement.Size = CalculateFileSize(Upload.ContentLength);
objCourseManagement.Guid = System.Guid.NewGuid().ToString();
objCourseManagement.CreatedBy = Convert.ToInt32((((SessionBasket)(Session["SessionHandler"])).UserID).ToString());
dsResult = objBAL_AUI_CourseManagement.Insert_Course_DocumentDetails(objCourseManagement);
if (dsResult.Tables[0].Rows.Count > 0)
{
const string fn = "window.parent.onCompleteFileVersion(1,'File uploaded successfully.','{0}','{1} of {2} Bytes','{3}','{4}');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(fn, fileName, Upload.UploadedLength, Upload.UploadedLength, dsResult.Tables[0].Rows[0]["Version"].ToString(), dsResult.Tables[0].Rows[0]["Size"].ToString()), true);
}
else
{
const string js = "window.parent.onComplete(1,'File uploaded successfully but Course not enter in Database .','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, fileName, Upload.UploadedLength, Upload.ContentLength), true);
}
}
else
{
string[] strDelSubfolder = Directory.GetDirectories(path);
foreach (string strFolderPath in strDelSubfolder)
{
DirectoryInfo dideldirectory = new DirectoryInfo(strFolderPath);
dideldirectory.Delete(true);
}
const string js = "window.parent.onComplete(4,'Mainfest File resources missing','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, fileName, Upload.UploadedLength, Upload.ContentLength), true);
}
}
#endregion
#region "Non scrom file database insert"
if (Upload.ScormCheckId == 1)
{
objCourseManagement.CourseId = Upload.CourseId;
objCourseManagement.FileName = fileName.Trim();
objCourseManagement.DocumentLink = strFileVirtualPath;
objCourseManagement.Size = CalculateFileSize(Upload.ContentLength);
objCourseManagement.Guid = System.Guid.NewGuid().ToString();
objCourseManagement.CreatedBy = Convert.ToInt32((((SessionBasket)(Session["SessionHandler"])).UserID).ToString());
dsResult = objBAL_AUI_CourseManagement.Insert_Course_DocumentDetails(objCourseManagement);
if (dsResult.Tables[0].Rows.Count > 0)
{
const string fn = "window.parent.onCompleteFileVersion(1,'File uploaded successfully.','{0}','{1} of {2} Bytes','{3}','{4}');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(fn, fileName, Upload.UploadedLength, Upload.UploadedLength, dsResult.Tables[0].Rows[0]["Version"].ToString(), dsResult.Tables[0].Rows[0]["Size"].ToString()), true);
}
else
{
const string js = "window.parent.onComplete(1,'File uploaded successfully but Course not enter in Database .','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, fileName, Upload.UploadedLength, Upload.ContentLength), true);
}
}
#endregion
}
#endregion
}
else
{
const string js = "window.parent.onComplete(4, 'There was a problem with the file.','','0 of 0 Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", js, true);
}
Upload.IsReady = false;
}
else
{
const string js = "window.parent.onComplete(4,'Course have been expired please close window and select again.','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, "NO file", "0", "0"), true);
}
}
else
{
ScriptManager.RegisterStartupScript(this, GetType(), "Upload Class Missing", "alert('Upload Missing')", true);
Response.Redirect("AUI_Login.aspx?OutUpload=ss", false);
}
}
#endregion
}
}
else
{
const string js = "window.parent.onComplete(4,'Session expired please login again.','{0}','{1} of {2} Bytes');";
ScriptManager.RegisterStartupScript(this, typeof(AUI_Course_UploadEngine), "progress", string.Format(js, "NO file", "0", "0"), true);
}
STEP : 2 Uploading File JAVASCRIPT CALL PAGE METHODS SERVER SIDE
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);
STEP : 3 Uploading File Process Bar % Completion
#region Web Methods
[System.Web.Services.WebMethod(EnableSession = true)]
[System.Web.Script.Services.ScriptMethod]
public static object GetUploadStatus()
{
UploadDetail info = (UploadDetail)HttpContext.Current.Session["UploadDetail"];
if (info != null && info.IsReady)
{
int soFar = info.UploadedLength;
int 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;
}
#endregion