I am facing an issue with uploading excel data to .Net control which is deployed on SharePoint 2010. The functionality is to upload the values that are in Excel sheet as comma separated to .Net Textbox Control. Before reading the data from Excel the document is saved to the server path (Server.Mappath) of the application. In my case it is the _Layouts of the SharePoint 2010 Server where all my ASPX pages are deployed. This functionality works fine on the SharePoint 2007 environment but the same functionality when deployed on SharePoint 2010 Environment is throwing an unauthorized exception. Below is the code i am using to upload the data from excel and error message. Can anyone please help me fixing this issue or any work around?
Error Message on SharePoint 2010:
Error Occured: System.UnauthorizedAccessException: Access to the path 'C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\template\layouts\Test\Dev\ABC.xlsx' is denied. at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at System.Web.HttpPostedFile.SaveAs(String filename) at App.Test.btnFileUpload_Click(Object sender, EventArgs e)
Code Sample:
if (fileUpload.HasFile)
{
strTarget = Server.MapPath(fileUpload.FileName);
string[] arrCheckExtension = strTarget.Split('.');
if (arrCheckExtension.Length >= 2)
{
if (arrCheckExtension[1].ToString().Equals("xls") || arrCheckExtension[1].ToString().Equals("xlsx"))
{
fileUpload.SaveAs(strTarget);
strConnForExcel = String.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0;HDR=YES;IMEX=1;""", strTarget);
strQueryForExcel = String.Format("select id from [{0}$]", "Test");
OleDbDataAdapter adap = new OleDbDataAdapter(strQueryForExcel, strConnForExcel);
ds = new DataSet();
adap.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if (strids == "")
{
strids += ds.Tables[0].Rows[i]["id"].ToString();
}
else
{
strids += "," + ds.Tables[0].Rows[i]["id"].ToString();
}
}
txtUpload.Text = strids;
}
}
else
{
Response.Write("<script language='javascript'>alert('Please Select File with .xls or xlsx Extension');</script>");
}
}
}
Dispose()
something. – SjipsPermissions
Issue if you are doing this via a web page then IIS_USER probably needs to have full read/right access not your local user account .. unless your admind allows all the users to have access to that share then the individual user's permissions need to be set I would start with that first I ran into the same thing locally if I change the file path to a local drive then everything works well in your case do the same but step past this linefileUpload.SaveAs(strTarget);
and see if the rest of the code works – MethodMantarget and strids
where are these defined.. – MethodMan