Click here to Skip to main content
15,889,281 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want how to upload file in a folder of server.
and how to define its path.

Please check.Below line is working for localhost but not working for web
SaveLocation=Server.MapPath("~/Kamna Group Website/"+fn);
Is it ok ?
Is this path is relevent to access image in Image1.ImageUrl

i am giving my coading

C#
void page_Load(Object sender, EventArgs e)
    {
    con = new OleDbConnection();
    con.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("~/kamnagroup.mdb") + "";
    con.Open();
    ds = new DataSet();
    string sql = "select photo,agent,point,aname,branch from joining WHERE point=(SELECT MAX(point) FROM joining)";
    da = new OleDbDataAdapter(sql, con);
    da.Fill(ds, "joining");
    DataRow dRow = ds.Tables["joining"].Rows[0];
    System.Web.UI.WebControls.Label lbl2 = new System.Web.UI.WebControls.Label();
    lbl2.Text = dRow.ItemArray.GetValue(0).ToString();
    lb1.Text = dRow.ItemArray.GetValue(1).ToString();
    lb2.Text = dRow.ItemArray.GetValue(2).ToString();
    lb3.Text = dRow.ItemArray.GetValue(3).ToString();
    lb4.Text = dRow.ItemArray.GetValue(4).ToString();
    fn = System.IO.Path.GetFileName(lbl2.Text.Trim()); Image1.ImageUrl = fn;
    con.Close();
    }
void sub(Object s, EventArgs e)
    {
    con.Open();
    Int32 x = 0; string y = GetRandomString(x);
    int y1 = 0;
    string y2 = "Code";
    string s1 = t1.Text.Trim();
    string m;
    int r = 0;
    DataRow[] rs;
    rs = ds.Tables["joining"].Select("aname='" + s1 + "'");
    r = rs.Length;
    if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0))
        {
        fn = System.IO.Path.GetFileName(File1.PostedFile.FileName);
        //Image1.ImageUrl=fn;
        fn = y + fn;
        //Response.Write(fn);
        //Response.Write("<br>");
        SaveLocation = Server.MapPath("~/Kamna Group Website/" + fn);
        photo = SaveLocation;
        com = con.CreateCommand();
        com.CommandText = "Insert into joining(aname,fathname,dob,pob,add1,mob,city,state,pcode,panno,nominee,relation,photo,bank,branch,account,acctype,intby,codno,doi,agent,point,refno) values('" + t1.Text.Trim() + "','" + t2.Text.Trim() + "','" + t3.Text.Trim() + "','" + t4.Text.Trim() + "','" + texta1.Value + "','" + t7.Text.Trim() + "','" + t5.Text.Trim() + "','" + t6.Text.Trim() + "','" + t8.Text.Trim() + "','" + t11.Text.Trim() + "','" + t9.Text.Trim() + "','" + t10.Text.Trim() + "','" + photo + "','" + t12.Text.Trim() + "','" + t13.Text.Trim() + "','" + t14.Text.Trim() + "','" + d1.SelectedItem.Text + "','" + t15.Text.Trim() + "','" + t16.Text.Trim() + "','" + t17.Text.Trim() + "','" + y2 + "','" + y1.ToString() + "','" + y + "')";
        try
            {
            File1.PostedFile.SaveAs(SaveLocation);
            com.ExecuteNonQuery();
            System.Web.UI.WebControls.Label lbl1 = new System.Web.UI.WebControls.Label();
            lbl1.ForeColor = System.Drawing.Color.Blue;
            lbl1.BackColor = System.Drawing.Color.Yellow;
            lbl1.Text = "You have joined us.We will contect you soon!";
            ph1.Controls.Add(lbl1);
            }
        catch (Exception ex)
            {
            Response.Write(ex.Message);
            }
        }
    else
        {
        Response.Write("Please a file to upload");
        }
    con.Close();
    }
private string GetRandomString(int seed)
    {
    const string alphabet = "abcdefghijklmnopqrstuvwxyz";
    Random rnd = new Random((seed + DateTime.Now.Millisecond));
    string result = rnd.Next(1000000, 9999999).ToString();
    string alphaChar = alphabet.Substring(rnd.Next(0, alphabet.Length - 1), 1);
    int replacementIndex = rnd.Next(0, (result.Length - 1));
    result = result.Remove(replacementIndex, 1).Insert(replacementIndex, alphaChar);
    return result;
    }


[edit]Code block added, "Treat my content as plain text..." option disabled - OriginalGriff[/edit]
Posted
Updated 4-Feb-12 0:29am
v2
Comments
OriginalGriff 4-Feb-12 6:31am    
Please do not repost - I have deleted to older version as this contains more info.
In future, use the "Improve question" widget to edit your question and provide better information.
Janardan Pandey 4-Feb-12 6:34am    
ok sorry but i need this so i reposted it.But help me to solve my problem.
OriginalGriff 4-Feb-12 6:48am    
Sorry, but no. I am not going to look at your code in too much detail, because it is so badly written it could be anything.
Stop leaving yourself wide open to SQL Injection attacks (i.e. convert it to use parametrized queries), stop using "t1", "t2" and so forth for your text boxes and use descriptive names instead, and explain what you are actually trying to do, and what doesn't work and why not, and I might. Otherwise, I would just be wasting my time looking for what your problem could be, and what could cause it.
Janardan Pandey 4-Feb-12 6:32am    
where is ur answer of my question of suggestion ?

1 solution

Hi,

I can't say that I understood what your problem is, but all I can see is that you have problems with paths.
The tilde (~) character represents the root directory of the application in ASP.NET.
If you want to use paths through code (in code-behind) I suggest you to use
C#
Request.ApplicationPath
and then append relative folder structure to it..
You have good article about ASP.NET paths here[^].
 
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