Why fileupload browsing file is not working in IIS server? In local it works fine.
This is my code:
FilePath = Server.MapPath(ConfigurationManager.AppSettings["CHFPath"]); //Server.MapPath("\\C3psps\\FileUpload\\CHFUpload\\");// + FUFile.FileName;
if (uploadFile.PostedFile != null)
{
//Build the name of the file to be uploaded as filepath + corporateId_CurrentDateTime
int startindex = uploadFile.PostedFile.FileName.LastIndexOf("\\");
fileName = uploadFile.PostedFile.FileName.Substring(startindex + 1);
//commented by biju on july 17 to suport excel 2007 aslo
// fileName = corporateId + "_" + Common.buildFileName() + ".xls";
//-----------------------------------
// added by biju on july 17 to support excel 2007 also in chf template
string fileType = fileName.Substring(fileName.LastIndexOf('.'));
//string FileExtension = System.IO.Path.GetExtension(uploadFile.PostedFile.FileName.ToString());
if (fileType == ".xls")
{
fileName = corporateId + "_" + Common.buildFileName() + ".xls";// txtFileName.Text.Substring(startindex + 1);
}
else
{
fileName = corporateId + "_" + Common.buildFileName() + ".xlsx";// txtFileName.Text.Substring(startindex + 1);
}
// ----------------------------------------------------
FilePath += fileName;
// Get a reference to PostedFile object
HttpPostedFile postedFile = uploadFile.PostedFile;
// Get size of uploaded file
int fileLen = postedFile.ContentLength;
//Check filesize > 10 MB
if (fileLen > (1048576 * 10))//1 KB = 1048576 bytes.So check for 10 KB
{
postedFile = null;
Common.showMessage("Uploaded file size could not exceed 10 MB.Please try again!", this);
return;
}
// make sure the size of the file is > 0
if (fileLen > 0)
{
// Allocate a buffer for reading of the file
byte[] fileData = new byte[fileLen];
// Read file from the Stream
postedFile.InputStream.Read(fileData, 0, fileLen);
// Uploading the file and saving in the server
postedFile.SaveAs(FilePath);
postedFile.InputStream.Flush();
postedFile.InputStream.Close();
postedFile = null;
}
}
#endregion
# region Check for CHF
MicrosoftExcelClient m_ExcelClient = null;
m_ExcelClient = new MicrosoftExcelClient(FilePath);
//Reset & Reopen Connection
m_ExcelClient.openConnection();
//Querry to check the uploaded excel file contains all required fields for CHF
string SqlSheetFetch = "";
SqlSheetFetch = "Select * From [Sheet1$] where ";
SqlSheetFetch += " EMPLOYEE_ID is not null or TITLE is not null ";
SqlSheetFetch += "or FIRST_NAME is not null or MIDDLE_NAME is not null ";
SqlSheetFetch += "or LAST_NAME is not null ";
SqlSheetFetch += "or PASSPORT_NO is not null or LABOUR_CARD_NO is not null ";
SqlSheetFetch += "or BUSINESS_PHONE is not null or MOBILE_NO is not null ";
SqlSheetFetch += "or EMAIL is not null or DOB is not null ";
SqlSheetFetch += "or MOTHERS_FIRST_NAME is not null or GENDER is not null or NATIONALITY is not null";
//Read the excel file
DataTable result = m_ExcelClient.readForSpecificQuery(SqlSheetFetch);
int count = 0;
m_ExcelClient.closeConnection();
m_ExcelClient = null;
string check = "";
if (result != null)
{
//Get the count of the columns in the uploaded excel
count = result.Columns.Count;
//Added by Alex on Nov 23.2013 as WPS (2 columns) were removed from the New CHF Template
if (count < 15)
{
new ManageEmployee().DeleteFile(FilePath);
Common.showMessage("Please upload a valid Employee List File!", this);
FilePath = "";
fileName = "";
return;
}
//Check whether the uploaded excel contains all the required fields
check = CheckCHFColumns(result);
}
//Delete the uploaded file and show error message if the excel file uploaded fails in validation
if (result == null || count != 15 || check == "-1")
{
new ManageEmployee().DeleteFile(FilePath);
Common.showMessage("Please upload a valid Employee List File!", this);
FilePath = "";
fileName = "";
return;
}
#endregion
//Insert the file info into DB for Queuing up
else
{
//Queue file
int errorCode = new ManageEmployee().QueueFile(corporateId, fileName, "C", 0, 0, "", 0, "", "", Session["USER"].ToString());
if (errorCode == 0)
{
Common.showMessage("File has been successfully queued up for saving as " + fileName, this);
Common.UserPageAudit(Session["User"].ToString(), "Manage Employees", Session["ROLE"].ToString(), "File has been successfully queued up for saving as '" + fileName+"'");
}
else
{
//Delete uploaded file from server location
new ManageEmployee().DeleteFile(FilePath);
Common.showMessage("Sorry, an error occured during the process!", this);
}
}
}
catch (Exception ex)
{
//Delete uploaded file from server location
new ManageEmployee().DeleteFile(FilePath);
Common.showMessage("Sorry,an error occured during the process" + "\n" + ex.Message, this);
FilePath = "";
fileName = "";
}
}
Here the path and permission case all are fine in server.