将excel文件上传到实时服务器上的文件夹时,会抛出异常“访问被拒绝的路径” [英] When uploding an excel file to a folder on live server it throws an exception "access to the path denied
本文介绍了将excel文件上传到实时服务器上的文件夹时,会抛出异常“访问被拒绝的路径”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
访问路径'C:\ HostingSpaces \sghitech \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ b
我尝试了什么:
Access to the path 'C:\HostingSpaces\sghitech\pwdpune.sghitech.co.in\wwwroot\ROAD STATSTICS SUB- DIVISION MARCH 2016.xls' is denied.
What I have tried:
public partial class StatisticsReport : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
string FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FilePath = Server.MapPath(FolderPath + FileName);
FileUpload1.SaveAs(FilePath);
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
}
}
private void Import_To_Grid(string FilePath, string Extension, string isHDR)
{
string conStr = "";
switch (Extension)
{
case ".xls": //Excel 97-03
conStr = ConfigurationManager.ConnectionStrings["Excel03ConString"].ConnectionString;
break;
case ".xlsx": //Excel 07
conStr = ConfigurationManager.ConnectionStrings["Excel07ConString"].ConnectionString;
break;
}
conStr = String.Format(conStr, FilePath, isHDR);
OleDbConnection connExcel = new OleDbConnection(conStr);
OleDbCommand cmdExcel = new OleDbCommand();
OleDbDataAdapter oda = new OleDbDataAdapter();
DataTable dt = new DataTable();
cmdExcel.Connection = connExcel;
//Get the name of First Sheet
connExcel.Open();
DataTable dtExcelSchema;
dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string SheetName = dtExcelSchema.Rows[1]["TABLE_NAME"].ToString();
connExcel.Close();
//Read Data from First Sheet
connExcel.Open();
cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";
oda.SelectCommand = cmdExcel;
oda.Fill(dt);
connExcel.Close();
//Bind Data to GridView
GridView1.Caption = Path.GetFileName(FilePath);
GridView1.DataSource = dt;
GridView1.DataBind();
}
protected void PageIndexChanging(object sender, GridViewPageEventArgs e)
{
string FolderPath = ConfigurationManager.AppSettings["FolderPath"];
string FileName = GridView1.Caption;
string Extension = Path.GetExtension(FileName);
string FilePath = Server.MapPath(FolderPath + FileName);
Import_To_Grid(FilePath, Extension, rbHDR.SelectedItem.Text);
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
}
我的Web.config文件中的
in my Web.config File
<appsettings>
<add key ="FolderPath" value ="Files/"/>
推荐答案
您需要让您的虚拟主机启用您网站的写入权限。如果您不需要公开访问这些文件,请将它们存储在App_Data文件夹中。如果您必须将它们存储在您的Web空间中,那么请考虑您正在执行的操作的安全隐患。当我上传aspx文件时会发生什么?更糟糕的是,当我上传default.aspx时会发生什么?我可以在你的服务器上上传和执行我想要的任何代码。
You'll need to ask your webhost to enable write permissions on your site. If you don't need these files to be publicly accessible then store them in the App_Data folder. If you must store them in your web space then think about the security implications of what you are doing. What happens when I upload an aspx file? Worse still what happens when I upload "default.aspx"? I can upload and execute any code on your server that I want.
这篇关于将excel文件上传到实时服务器上的文件夹时,会抛出异常“访问被拒绝的路径”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文