Click here to Skip to main content
15,904,348 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am using a file upload control and using some java scripts I have made it click function to do the code behind thing too using another adjacent button. The problem is that I want to get the full path of the file from user's machine so that I can perform some validations over it and than finally save it into database. However, I am not able to do so as the path I am getting using server.mappath() means the path of server and thus code gives a defect in the path. Could you please tell me how to get the full file path and than saving it to database. lets say the file is a excel file.
My code here :

C#
 protected void Button1_Click(object sender, EventArgs e)
    {

        try
        {

            if (FileUpload1.HasFile)
            {
                string strFilepath = Path.GetFullPath(FileUpload1.PostedFile.FileName);
                string filename =(FileUpload1.PostedFile.FileName);
                                if (filename != "")
                {
                    res = ImportExcel2007(filename);
                    if (res != null)
                    {
                        gvErrors.DataSource = res;
                        gvErrors.DataBind();
                    }
                }
            }
        }



        catch (Exception ex)
        {
            throw ex;
        }
    }

private DataSet ImportExcel2007(String strFilePath)
    {
        //if (!File.Exists(strFilePath)) return false;
        String strExcelConn = "Provider=Microsoft.ACE.OLEDB.12.0;"
        + "Data Source=" + strFilePath + ";"
        + "Extended Properties='Excel 8.0;HDR=No'";
        OleDbConnection connExcel = new OleDbConnection(strExcelConn);
        OleDbCommand cmdExcel = new OleDbCommand();
        DataSet ds = new DataSet();
        try
        {
            cmdExcel.Connection = connExcel;

            //Check if the Sheet Exists
            connExcel.Open();
            DataTable dtExcelSchema;
            //Get the Schema of the WorkBook
            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
            connExcel.Close();

            //Read Data from Sheet1
            connExcel.Open();
            OleDbDataAdapter da = new OleDbDataAdapter();

            Error Comes here ( No row at postion[0]) --> string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();
            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";

            //Range Query
            //cmdExcel.CommandText = "SELECT * From [" + SheetName + "A3:B5]";

            da.SelectCommand = cmdExcel;
            da.Fill(ds);
            connExcel.Close();

        }
        catch(Exception ex)
        {
            throw ex;

        }
        finally
        {
            cmdExcel.Dispose();
            connExcel.Dispose();
        }

        return ds;
    }

Pls help !
Posted

Use
C#
Server.MapPath("~/App_Data/")
and save the file to the website App_Data folder. Then you will have no problem in reading it
 
Share this answer
 
Try this...

C#
if (FileUpload1.HasFile)
{
    FileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    string Extension = Path.GetExtension(FileUpload1.PostedFile.FileName);
    string FolderPath = ConfigurationManager.AppSettings["DMSFolder"];
    FilePath = Server.MapPath(FolderPath + FileName);
    FileUpload1.SaveAs(FilePath);
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900