0

I have an web application and I want to upload an excel file and read it in the application. Everything is good when I run the application with VS or in localhost(with windows 7 and IIS 7.5). But when I deploy the application in an server with windows server 2008 and IIS 7.5 I see two errors. I connected to the server with team viewer and I deployed the application on the server.

When I want to upload the file from the server's desktop I see this (I have the admin access) :

Access to the path 'C:\Users\Administrator\Desktop\931001.xlsx' is denied.

And when I want to upload from another pathese I see this error:

The given path's format is not supported.

These are my tries to upload the file:

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

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

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

What could the problem be?

Godrej
  • 15
  • 1
  • 1
  • 6
Hamid Reza
  • 2,913
  • 9
  • 49
  • 76

1 Answers1

0

Try use it as follows:

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();
}
Tomer Klein
  • 436
  • 2
  • 6