UploadFilesUtility
/*************************************************************************************
CREATED BY : Navin Kumar
CREATED ON : 16-Nov-2016
SUMMARY : Created for upload files.
MODIFIED HISTORY :
S.NO MODIFIED ON MODIFIED BY TRACKER SUMMARY
1 <DATE> <NAME> <MINDBS20160919> <SUMMARY>
*******************************************************************************************/
using System;
using System.Web;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using iGHS.Web.Sys.Infrastructure.Framework;
using iGHS.Shared.Repositories.Helpers;
using iGHS.Web.Sys.Utilities.ExcelHelper;
using iGHS.Shared.GlobalSettings;
using iGHS.Shared.Enums.GlobalSetting;
namespace iGHS.Web.Sys.Infrastucture.Common
{/// <summary>
///
/// </summary>
public class UploadFile
{
HttpPostedFileBase _file;
/// <summary>
/// Initializes a new instance of the <see cref="UploadFile"/> class.
/// </summary>
/// <param name="file">The file.</param>
public UploadFile(HttpPostedFileBase file)
{ _file = file; }
/// <summary>
/// Gets or sets the file path.
/// </summary>
/// <value>
/// The file path.
/// </value>
public string filePath { get; set; }
/// <summary>
/// Gets or sets the type of the file.
/// </summary>
/// <value>
/// The type of the file.
/// </value>
public string fileType { get; set; }
/// <summary>
/// Gets or sets the name of the table.
/// </summary>
/// <value>
/// The name of the table.
/// </value>
public string TableName { get; set; }
/// <summary>
/// Gets or sets the name of the sheet.
/// </summary>
/// <value>
/// The name of the sheet.
/// </value>
public string SheetName { get; set; }
/// <summary>
/// Gets or sets the file extension.
/// </summary>
/// <value>
/// The file extension.
/// </value>
public string FileExtension { get; set; }
/// <summary>
/// Gets or sets a value indicating whether [isfist row is first row as column names].
/// </summary>
/// <value>
/// <c>true</c> if [isfist row is first row as column names]; otherwise, <c>false</c>.
/// </value>
public Boolean IsfistRowIsFirstRowAsColumnNames { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is verification flag.
/// </summary>
/// <value>
/// <c>true</c> if this instance is verification flag; otherwise, <c>false</c>.
/// </value>
public Boolean IsVerificationFlag { get; set; }
/// <summary>
/// Gets or sets a value indicating whether this instance is local.
/// </summary>
/// <value>
/// <c>true</c> if this instance is local; otherwise, <c>false</c>.
/// </value>
public Boolean IsLocal { get; set; }
/// <summary>
/// Gets or sets the session identifier.
/// </summary>
/// <value>
/// The session identifier.
/// </value>
public string SessionId { get; set; }
/// <summary>
/// Gets or sets the user identifier.
/// </summary>
/// <value>
/// The user identifier.
/// </value>
public string UserId { get; set; }
/// <summary>
/// Gets or sets the name of the form.
/// </summary>
/// <value>
/// The name of the form.
/// </value>
public string FormName { get; set; }
/// <summary>
/// Gets or sets the name of the pad in file.
/// </summary>
/// <value>
/// The name of the pad in file.
/// </value>
public string PadInFileName { get; set; }
/// <summary>
/// Saves the upload file.
/// </summary>
/// <returns></returns>
public string SaveUploadFile()
{
System.IO.DirectoryInfo BackDir;
try
{
string fileName;
var file = _file;
var timeStamp = DateTime.Now.ToString("yyyyMMddHHmmssfff");
fileName = Path.GetFileName(file.FileName);
FileExtension = Path.GetExtension(fileName).Replace(".", "");
fileName = fileName.Replace(FileExtension, "").Replace(".", "");
if (filePath == null || filePath == "")
{
return WebApplication.GetAppResource("20419");
}
if (fileType == null || fileType == "")
{
fileType = "EXCEL";
}
if (TableName == null || TableName == "")
{
return WebApplication.GetAppResource("20420");
}
if (SessionId == null || SessionId == "")
{
return WebApplication.GetAppResource("20421");
}
if (UserId == null || UserId == "")
{
return WebApplication.GetAppResource("20422");
}
if (fileType.ToUpper() == "EXCEL")
{
if (FileExtension.ToLower() != "xls" && FileExtension.ToLower() != "xlsx")
{
return WebApplication.GetAppResource("20423") + fileName;
}
}
if (PadInFileName != null && PadInFileName != "")
{
PadInFileName = PadInFileName + "_";
}
var strModifiedFileName = fileName + "_" + FormName + "_" + PadInFileName + UserId + "_" + timeStamp + "." + FileExtension;
var FileNamewithPath = filePath + strModifiedFileName;
var FileNamewithBatckupPath = filePath + "\\BACKUP\\" + strModifiedFileName;
BackDir = new System.IO.DirectoryInfo(filePath + "\\BACKUP\\");
if (BackDir.Exists == false)
{
BackDir.Create();
}
FileNamewithPath = FileNamewithPath.Replace("'", "_");
FileNamewithPath = FileNamewithPath.Replace(";", "_");
FileNamewithPath = FileNamewithPath.Replace(",", "_");
FileNamewithPath = FileNamewithPath.Replace("`", "_");
FileNamewithPath = FileNamewithPath.Replace("^", "_");
if (IsLocal == false)
{
using (new Impersonator(WebApplication.GetFWConfiguration("Application.Username"), WebApplication.GetFWConfiguration("Application.Domain"), WebApplication.GetFWConfiguration("Application.Password")))
{
file.SaveAs(FileNamewithPath);
if (fileType.ToUpper() == "EXCEL")
{
if (DataProcess(FileNamewithPath) == false)
{
return WebApplication.GetAppResource("20423");
}
else
{
System.IO.File.Move(FileNamewithPath, FileNamewithBatckupPath);
}
}
}
}
else
{
file.SaveAs(FileNamewithPath);
if (fileType.ToUpper() == "EXCEL")
{
if (DataProcess(FileNamewithPath) == false)
{
return WebApplication.GetAppResource("20423");
}
else
{
System.IO.File.Move(FileNamewithPath, FileNamewithBatckupPath);
}
}
}
return "DONE";
}
catch
{
throw;
}
}
/// <summary>
/// Datas the process.
/// </summary>
/// <param name="FileNamewithPath">The file namewith path.</param>
/// <returns></returns>
private bool DataProcess(string FileNamewithPath)
{
DataSet result = null;
IExcelDataReader excelReader = null;
FileStream stream = null;
try
{
if (System.IO.File.Exists(FileNamewithPath))
{
if (TableName != "")
{
stream = System.IO.File.Open(FileNamewithPath, FileMode.Open, FileAccess.Read);
if (FileExtension.ToLower() == "xlsx")
{
excelReader = ImportByOpenXML.CreateReader(stream, ExcelFileType.OpenXml);
}
else
{
excelReader = ImportByOpenXML.CreateReader(stream, ExcelFileType.Binary);
}
excelReader.IsFirstRowAsColumnNames = true;
excelReader.SheetName = SheetName;
result = excelReader.AsDataSet(true);
return DataTransfer(result);
}
return false;
}
return true;
}
catch
{
throw;
}
finally
{
if (result != null)
result.Dispose();
if (excelReader != null)
excelReader.Dispose();
if (stream != null)
stream.Dispose();
}
}
/// <summary>
/// Datas the transfer.
/// </summary>
/// <param name="result">The result.</param>
/// <returns></returns>
private bool DataTransfer(DataSet result)
{
SqlHelper sqlhelp = null;
SqlBulkCopy bulkCopy = null;
string constr = new GlobalSettingsManager().Get<string>(nameof(GlobalSettingKeys.ConnectionString));//System.Configuration.ConfigurationManager.ConnectionStrings["DbConnection"].ConnectionString;
DataColumn column = default(DataColumn);
try
{
if (result != null && result.Tables.Count > 0)
{
if (result.Tables[0].Rows.Count > 0)
{
sqlhelp = new SqlHelper(constr);
//sqlhelp.ExecuteNonQuery("DELETE FROM " + TableName + " WHERE UPLOADED_BY = '" + UserId + "' AND SESSION_ID = '" + SessionId + "' ", CommandType.Text);
column = new DataColumn();
var _with1 = column;
_with1.DataType = System.Type.GetType("System.String");
_with1.DefaultValue = UserId;
result.Tables[0].Columns.Add(column);
// Add column for Uploaded DateTime
column = new DataColumn();
var _with2 = column;
_with2.DataType = System.Type.GetType("System.DateTime");
_with2.DefaultValue = System.DateTime.Now;
result.Tables[0].Columns.Add(column);
// Add column for Session ID
column = new DataColumn();
var _with3 = column;
_with3.DataType = System.Type.GetType("System.String");
_with3.DefaultValue = SessionId;
result.Tables[0].Columns.Add(column);
if (IsVerificationFlag)
{
DataRow drr;
drr = result.Tables[0].Rows[0];
result.Tables[0].Rows.Remove(drr);
}
SqlConnection _dconn = new SqlConnection(constr);
_dconn.Open();
bulkCopy = new SqlBulkCopy(_dconn);
bulkCopy.BatchSize = 100000;
bulkCopy.BulkCopyTimeout = 1000;
bulkCopy.DestinationTableName = TableName;
bulkCopy.WriteToServer(result.Tables[0].CreateDataReader());
_dconn.Close();
}
}
}
catch
{
throw;
}
finally
{
if (sqlhelp != null)
sqlhelp.Dispose();
if (result != null)
result.Dispose();
if (bulkCopy != null)
bulkCopy.Close();
}
return true;
}
}
}
Comments
Post a Comment