asp.net-mvciis-8devexpress-mvc

facing error while uploading file in mvc5 on server


I have MVC5 application , where I need to upload file excel and then then create data table of this excel. I use devexpress control to upload file. I use following code to store uploded file and then read in datatable and then store in database.

 if (e.UploadedFile.IsValid)
        {
            e.UploadedFile.SaveAs(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + e.UploadedFile.FileName));
            var Filepath = System.IO.Path.Combine(System.Web.HttpContext.Current.Server.MapPath("~/App_Data/" + e.UploadedFile.FileName));
            DataTable  dtReport = new DataTable();
            try
            {
                dtReport = CreateDataTableFromExcelFile(Filepath, "A1:U", true, "Sheet1").Tables[0];
            }
            catch
            {
            }
              SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con.Open();
            System.Data.SqlClient.SqlBulkCopy sqlcopy = new System.Data.SqlClient.SqlBulkCopy(con);
            sqlcopy.DestinationTableName = "table_Name";
            sqlcopy.WriteToServer(dtReport);
            con.Close();

If I host this application on IIS on my machine then it works fine. But If I host it on another server then it shows error

Access to the path 'C:\inetpub\wwwroot\MVC_Project_v3\App_Data\UploadTemp\dxupload_19aafa62643d42418b2fe5eaadede3cfcugxrc4e.nrt.tmp' is denied.

Please suggest right solution


Solution

  • The credential you are using to host the WebSite (the credential you enter in the application pool identity) does not have the privilege to access that directory.

    enter image description here

    Add a valid credential here.