Click here to Skip to main content
15,895,807 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.
Posted
Updated 20-Feb-14 0:25am
v2
Comments
Rahul 105 21-Feb-14 7:03am    
Awaiting a reply on the same . We have set the path of each files as below in the web.config. But File Uploading isn't working in the server suddenly of late.
<add key="CHF_ErrorLogPath" value="\C3psps\FileUpload\CHF_BLOCKErrorLog\">
<add key="WORKING_FOLDER_CHF" value="\C3psps\FileUpload\CHF_WorkingFolder\">
<add key="CHFTEXT_BLOCK_FILE_ARCHIVE" value="\C3psps\FileUpload\CHF_TextFileArchive\">
<add key="MASS_CMS_UPDATE_FILE_PATH" value="\C3psps\FileUpload\MassUpdation\">

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900