簡體   English   中英

Windows服務器不支持給定路徑的格式

[英]The given path's format is not supported in windows server

我有一個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.

當我想從另一個上傳時,我看到了這個錯誤:

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();
}

問題是什么?

嘗試使用如下:

 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(); } 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM