filestreamer cannot read the uploaded file in the folder -

Dec 13 2019 4:35 AM
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.IO;
using System.Data.Linq;
using System.Security;
using System.Web.Security;
using System.Security.Permissions;
using AppCode.AL;
using AppCode.Model;
using AppCode.IL;
public partial class Document : System.Web.UI.Page
{
DataTable dt = new DataTable();
IDocument documentUC;
ListItemCollection collection;
private const int _firstEditCellIndex = 1;
IList<MDM_Location> locList = new List<MDM_Location>();
//string filePath = ConfigurationManager.AppSettings["UploadFileFolder"];
string directory = ConfigurationManager.AppSettings["FileDirectory"].Trim();
string extention = "";
string[] tokens;
protected void Page_Load(object sender, EventArgs e)
{
#region "Set Calender Position"
calCapture.Attributes.Add("style", "Z-INDEX: 176; LEFT: 584px; POSITION: absolute; TOP: 176px");
#endregion
#region "Session handling for Security"
if (Session["employee"] != null)
{
string userRole = Session["userName"].ToString();
if (Roles.IsUserInRole(userRole, "intiatedocument") != true)
{
Response.Redirect("~/Account/Login.aspx");
}
}
else
{
Response.Redirect("~/Account/Login.aspx");
}
#endregion
if (!IsPostBack )
{
LoadDropDownData();
locList = documentUC.GetAlllLocations();
gvLocations.DataSource = locList;
gvLocations.DataBind();
}
}
#region "Private Methods"
#endregion
void LoadDropDownData()
{
try
{
documentUC = new DocumentUC();
IList<MDM_Location> locList = new List<MDM_Location>();
locList = documentUC.GetAlllLocations();
if (locList.Count > 0)
{
collection = new ListItemCollection();
for (int i = 0; i < locList.Count; i++)
{
collection.Add(new ListItem(locList[i].LocationName.ToString(), locList[i].Id.ToString(), true));
}
ddlInitiateLocation.Items.AddRange(collection.Cast<ListItem>().ToArray());
ddlInitiateLocation.SelectedIndex = int.Parse(Session["location"].ToString());
// ddlInitiateLocation.DataBind();
}
IList<MDM_DocumentType> docList = new List<MDM_DocumentType>();
docList = documentUC.GetAllDocumentTypes();
if (docList.Count > 0)
{
collection = new ListItemCollection();
for (int k = 0; k < docList.Count; k++)
{
collection.Add(new ListItem(docList[k].TypeName.ToString(), docList[k].Id.ToString()));
}
ddlDocType.Items.AddRange(collection.Cast<ListItem>().ToArray());
//ddlDocType.DataSource = collection;
//ddlDocType.DataBind();
}
}
catch (Exception ex)
{
throw ex;
}
}
protected void btnSave_Click(object sender, EventArgs e)
{
bool locationChecked = false;
try
{
DDS_Document doc = new DDS_Document();
#region "Upload file to specific location in server"
if (string.IsNullOrEmpty(fluDocument.FileName))
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", "Please select file to Upload"), true);
return;
}
if (fluDocument.HasFile)
{
string transferfile = "";
transferfile = fluDocument.FileName;
fluDocument.PostedFile.SaveAs(directory + transferfile);
//fluDocument.PostedFile.SaveAs(filePath + transferfile);
//FileUpload1.SaveAs(directory + filename);
}
else
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", "File Could not be Updated"), true);
return;
}
#endregion
if (!string.IsNullOrEmpty(fluDocument.FileName))
{
tokens = fluDocument.PostedFile.FileName.Split(new string[] { "." }, StringSplitOptions.None);
extention = tokens[1];
FileStream fs = new FileStream(directory + fluDocument.PostedFile.FileName, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
br.Close();
fs.Close();
doc.DocumentContent = bytes;
#region "Checking extention"
if (extention == "pdf")
{
doc.ContentType = "application/" + extention;
}
if (extention == "jpg")
{
doc.ContentType = "image/" + extention;
}
if (extention == "xls")
{
doc.ContentType = "application/vnd.ms-excel";
}
if (extention == "xlsx")
{
doc.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
}
if (extention == "docx")
{
doc.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
}
if (extention == "doc")
{
doc.ContentType = "application/doc";
}
#endregion
if (File.Exists(directory + fluDocument.PostedFile.FileName))
{
File.Delete(directory + fluDocument.PostedFile.FileName);
}
}
doc.CaptureDate = Convert.ToDateTime (txtDate.Text);
doc.CaptureLocation = int.Parse(Session["location"].ToString());
doc.RefDocType = int.Parse(ddlDocType.SelectedValue);
doc.FileName = fluDocument.PostedFile.FileName;
doc.DocumentStatus = (int)DocumentTypes.InitiateDocument;
//Add Location Details
EntitySet<DDS_DocumentLocation> locList = new EntitySet<DDS_DocumentLocation>();
foreach (GridViewRow row in gvLocations.Rows)
{
DDS_DocumentLocation loc = new DDS_DocumentLocation();
CheckBox ddlId = (CheckBox)row.FindControl("chkItem");
if (ddlId!=null && ddlId.Checked)
{
Label txtLocation = (Label)row.FindControl("lblLocationDate");
loc.DispatchedLocation = int.Parse(txtLocation.Text);
loc.RefDocument = doc.Id;
locList.Add(loc);
locationChecked = true;
}
}
if (locationChecked==false)
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", "Please select Atleast one Dispatch location"), true);
return;
}
//End Add locations
doc.DDS_DocumentLocations = locList;
//Add Document Controls
EntitySet<DDS_DocumentControlRecord> docControlList = new EntitySet<DDS_DocumentControlRecord>();
DDS_DocumentControlRecord docControl = new DDS_DocumentControlRecord();
docControl.RefDocId = doc.Id;
docControl.RecordType = (int)DocumentTypes.InitiateDocument;
MDM_Employee emp = (MDM_Employee)Session["employee"];
docControl.RefEmp = emp.Id;
docControl.RecordDate = Convert.ToDateTime(DateTime.Now.ToShortDateString());
docControlList.Add(docControl);
doc.DDS_DocumentControlRecords = docControlList;
documentUC = new DocumentUC();
if (documentUC.SaveDocument(doc, "Capture"))
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", "Document Sucessfully Saved"), true);
return;
}
}
catch (Exception ex)
{
ScriptManager.RegisterStartupScript(Page, this.GetType(), "alert", string.Format("alert('{0}');", ex), false);
return;
}
}
protected void btnView_Click(object sender, EventArgs e)
{
}
protected void btnDate_Click(object sender, EventArgs e)
{
calCapture.Visible = true;
}
protected void calCapture_SelectionChanged(object sender, EventArgs e)
{
txtDate.Text = calCapture.SelectedDate.ToString("MM-dd-yyyy");
calCapture.Visible = false;
}
}

Answers (1)