【问题标题】:The given path's format is not supported in windows serverWindows 服务器不支持给定路径的格式
【发布时间】: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


    【解决方案1】:

    尝试如下使用:

    public ActionResult Index(HttpPostedFileBase file)
    {
        var path = Path.Combine(Server.MapPath("~/Files/",file.FileName));
        file.SaveAs(path);
        var connectionString = "";
        if (Path.GetExtension(path).ToLower()==".xslx")
        {
            connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;",path);
        }
        else
        {
            connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", path);
        }
        var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
        var ds = new DataSet();
        adapter.Fill(ds, "customer");
        var customer = ds.Tables["customer"].AsEnumerable();
    }

    【讨论】:

      猜你喜欢
      • 2011-11-13
      • 2012-04-21
      • 1970-01-01
      • 2017-09-14
      • 2014-12-08
      • 1970-01-01
      • 1970-01-01
      • 2012-05-15
      相关资源
      最近更新 更多