【发布时间】:2023-03-31 07:27:01
【问题描述】:
我有一个 Web 应用程序,我想上传一个 excel 文件并在应用程序中读取它。当我使用 VS 或在 localhost(使用 Windows 7 和 IIS 7.5)运行应用程序时,一切都很好。但是,当我将应用程序部署在具有 Windows Server 2008 和 IIS 7.5 的服务器中时,我看到了两个错误。我使用团队查看器连接到服务器,并在服务器上部署了应用程序。
当我想从服务器桌面上传文件时,我看到了这个(我有管理员权限):
Access to the path 'C:\Users\Administrator\Desktop\931001.xlsx' is denied.
当我想从另一个 pathese 上传时,我看到了这个错误:
The given path's format is not supported.
这些是我上传文件的尝试:
尝试 1:
public ActionResult Index(HttpPostedFileBase file)
{
file.SaveAs(Server.MapPath("~/Files/" + file.FileName));
var fileName = "/Files/" + file.FileName;
var connectionString = "";
if (file.FileName.Split('.').LastOrDefault().ToLower() == "xlsx")
{
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", Server.MapPath(fileName));
}
else
{
connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
}
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "contracts");
var contracts = ds.Tables["contracts"].AsEnumerable();
}
尝试 2:
public ActionResult Index(HttpPostedFileBase file)
{
var path = Server.MapPath("~/Files/");
file.SaveAs(Path.Combine(path, file.FileName));
var fileName = "~/Files/" + file.FileName;
var connectionString = "";
if (file.FileName.Split('.').LastOrDefault().ToLower() == "xlsx")
{
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", Server.MapPath(fileName));
}
else
{
connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", Server.MapPath(fileName));
}
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "customer");
var customer = ds.Tables["customer"].AsEnumerable();
}
尝试 3:
public ActionResult Index(HttpPostedFileBase file)
{
var path = Server.MapPath(@"\Files\");
file.SaveAs(Path.Combine(path, file.FileName));
var fileName = @"\Files\" + file.FileName;
var connectionString = "";
if (file.FileName.Split('.').LastOrDefault().ToLower() == "xlsx")
{
connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", Server.MapPath(fileName));
}
else
{
connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", Server.MapPath(fileName));
}
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
var ds = new DataSet();
adapter.Fill(ds, "customer");
var customer = ds.Tables["customer"].AsEnumerable();
}
可能是什么问题?
【问题讨论】:
标签: c# asp.net-mvc windows-server-2008-r2