为什么fileupload浏览文件在IIS服务器中不起作用? [英] Why fileupload browsing file is not working in IIS server?
问题描述
为什么fileupload浏览文件在IIS服务器中不起作用?在本地它工作正常。
这是我的代码:
FilePath = Server.MapPath(ConfigurationManager.AppSettings [CHFPath]); //Server.MapPath(\"\\C3psps \\FileUpload\\CHFUpload \\); // + FUFile.FileName;
if(uploadFile.PostedFile!= null)
{
//构建要上传的文件名作为filepath + corporateId_CurrentDateTime
int startindex = uploadFile.PostedFile.FileName.LastIndexOf(\\);
fileName = uploadFile.PostedFile.FileName.Substring(startindex + 1);
// 7月17日由biju评论为支持excel 2007 aslo
// fileName = corporateId +_+ Common.buildFileName()+。xls;
// --------------- --------------------
//由biju于7月17日添加以支持excel 2007 chf模板
string fileType = fileName.Substring(fileName.LastIndex ('。'));
//字符串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;
//得到推荐发表文件对象
HttpPostedFile postedFile = uploadFile.PostedFile;
//获取上传文件的大小
int fileLen = postingFile.ContentLength;
//检查filesize> 10 MB
if(fileLen>(1048576 * 10))// 1 KB = 1048576 bytes。检查10 KB
{
postedFile = null;
Common.showMessage(上传的文件大小不能超过10 MB。请再试一次!,这个);
返回;
}
//确保文件大小为> 0
if(fileLen> 0)
{
//分配缓冲区以读取文件
byte [] fileData = new byte [fileLen];
//从Stream中读取文件
//上传文件并保存在服务器中
postedFile.SaveAs(FilePath);
postedFile.InputStream .Flush();
postedFile.InputStream.Close();
postedFile = null;
}
}
#endregion
#region检查CHF
MicrosoftExcelClient m_ExcelClient = null;
m_ExcelClient = new MicrosoftExcelClient(FilePath);
//重置并重新开启连接
m_ExcelClient.openConnection();
// Querry检查上传的excel文件包含CHF的所有必填字段
string SqlSheetFetch =;
SqlSheetFetch =选择*来自[Sheet1 $]其中;
SqlSheetFetch + =EMPLOYEE_ID不为空或TITLE不为空;
SqlSheetFetch + =或FIRST_NAME不为空或MIDDLE_NAME不是null;
SqlSheetFetch + =或LAST_NAME不为空;
SqlSheetFetch + =或PASSPORT_NO不为空或LABOUR_CARD_NO不为空;
SqlSheetFetch + =或BUSINESS_PHONE不为空或MOBILE_NO不为空;
SqlSheetFetch + =或EMAIL不为空或DOB不为空;
SqlSheetFetch + =或MOTHERS_FIRST_NAME不为空或GENDER不是零l或NATIONALITY不为空;
//读取excel文件
DataTable结果= m_ExcelClient.readForSpecificQuery(SqlSheetFetch);
int count = 0;
m_ExcelClient.closeConnection();
m_ExcelClient = null;
string check =;
if(result!= null)
{
/ /获取上传的excel中的列数
count = result.Columns.Count;
//由Alex于2013年11月23日添加为WPS(2列)从新的CHF模板中删除
if(count< 15)
{
新的ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(请上传有效的员工列表文件! ,这个);
FilePath =;
fileName =;
return;
}
//检查上传的excel是否包含所有必填字段
check = CheckCHFColumns(result);
}
//如果上传的excel文件在验证中失败,请删除上传的文件并显示错误消息
if(result == null || count!= 15 || check == - 1)
{
new ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(请上传有效的员工列表文件!,这个);
FilePath = ;
fileName =;
返回;
}
#endregion
//将文件信息插入DB进行排队
else
{
//队列文件
int errorCode = new ManageEmployee()。QueueFile(corporateId,fileName,C,0,0,,0,,,Session [USER]。ToString ());
if(errorCode == 0)
{
Common.showMessage(文件已成功排队等待保存为+ fileName,this);
Common.UserPageAudit(Session [User]。ToString(),Manage Employees,Session [ROLE]。ToString(),File has been成功排队等待保存为'+ fileName +');
}
else
{
//删除从服务器位置加载文件
新ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(抱歉,在此过程中发生错误!,这个);
}
}
}
catch(exception ex)
{
//从服务器位置删除上传的文件
new ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(抱歉,在此过程中发生错误+\ n+ ex.Message,这个);
FilePath =;
fileName =;
}
}
这里的路径和权限在服务器中一切都很好。
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.
推荐答案
其中;
SqlSheetFetch + =EMPLOYEE_ID不为空或TITLE不为空;
SqlSheetF etch + =或FIRST_NAME不为空或MIDDLE_NAME不为空;
SqlSheetFetch + =或LAST_NAME不为空;
SqlSheetFetch + =或PASSPORT_NO是不为null或LABOUR_CARD_NO不为空;
SqlSheetFetch + =或BUSINESS_PHONE不为空或MOBILE_NO不为空;
SqlSheetFetch + =或EMAIL不为null或DOB不为空;
SqlSheetFetch + =或MOTHERS_FIRST_NAME不为空或GENDER不为null或NATIONALITY不为空;
//读取excel文件
DataTable结果= m_ExcelClient.readForSpecificQuery(SqlSheetFetch);
int count = 0;
m_ExcelClient.closeConnection();
m_ExcelClient = null;
string check =;
if(result!= null)
{
//获取上传的excel中的列数
count = result.Columns.Count;
// Alex于2013年11月23日添加为WPS(2列)从新的CHF模板中删除了
if(count< 15)
{
新的ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(请上传有效的员工列表文件! ,这个);
FilePath =;
fileName =;
return;
}
//检查上传的excel是否包含所有必填字段
check = CheckCHFColumns(result);
}
//如果上传的excel文件在验证中失败,请删除上传的文件并显示错误消息
if(result == null || count!= 15 || check == - 1)
{
new ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(请上传有效的员工列表文件!,这个);
FilePath = ;
fileName =;
返回;
}
#endregion
//将文件信息插入DB进行排队
else
{
//队列文件
int errorCode = new ManageEmployee()。QueueFile(corporateId,fileName,C,0,0,,0,,,Session [USER]。ToString ());
if(errorCode == 0)
{
Common.showMessage(文件已成功排队等待保存为+ fileName,this);
Common.UserPageAudit(Session [User]。ToString(),Manage Employees,Session [ROLE]。ToString(),File has been成功排队等待保存为'+ fileName +');
}
else
{
//删除从服务器位置加载文件
新ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(抱歉,在此过程中发生错误!,这个);
}
}
}
catch(exception ex)
{
//从服务器位置删除上传的文件
new ManageEmployee()。DeleteFile(FilePath);
Common.showMessage(抱歉,在此过程中发生错误+\ n+ ex.Message,这个);
FilePath =;
fileName =;
}
}
这里的路径和权限案例在服务器上都很好。
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.
这篇关于为什么fileupload浏览文件在IIS服务器中不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!