Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
what is the path to upload a image on the web serve of my asp.net project ?

the image is shown when the project running in local host but when i hosted it on the web server.It is not showing.

I am giving the program to upload a file in local host.

Please help me what is the path should be for web site.


C#
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; 
    }
Posted
Updated 5-Feb-12 18:49pm
v2

1 solution

simly use as
check bold lines
C#
protected void btnSub_Click(object sender, EventArgs e)
    {
        try
        {
            string filename = FileUpload1.FileName;
           
            FileUpload1.PostedFile.SaveAs(Server.MapPath("~\\Uploadform\\" + filename.Trim()));
          
            string path = "~\\Uploadform\\" + filename.Trim();
         
            SqlConnection con = new SqlConnection(str);
            cmd = new SqlCommand("Insert into Circular(date,ctype,title,type,url1) values('"+txtdate.Text+"','" + TextBox1.Text + "','" + txttitle.Text + "','" + txttype.Text + "','" + path + "')", con);
            lblinfo.Text = " Uploaded Successfully ";
            cmd.CommandType = CommandType.Text;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
    }
 
Share this answer
 
Comments
Janardan Pandey 6-Feb-12 1:19am    
how to define FileUpload1 control
uspatel 6-Feb-12 1:24am    
What is File1 in your code?
I think it is FileUpload control.
Otherwise how you are uplodaing File?
Janardan Pandey 6-Feb-12 1:28am    
ok i found it
Janardan Pandey 6-Feb-12 1:49am    
Thank u but help me once more how to display uploaded image in our web page during page load when we uploaded it to our web server ? Please help me ....

My program is given below which i developed.Please see and help me if any erro have it.


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,refno 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();
System.Web.UI.WebControls.Label lbl3=new System.Web.UI.WebControls.Label();
lbl2.Text =dRow.ItemArray.GetValue(0).ToString();
lb2.Text = dRow.ItemArray.GetValue(1).ToString();
lb3.Text = dRow.ItemArray.GetValue(2).ToString();
lb4.Text = dRow.ItemArray.GetValue(3).ToString();
lb5.Text = dRow.ItemArray.GetValue(4).ToString();
lbl3.Text = dRow.ItemArray.GetValue(5).ToString();
ds1=new DataSet();
string sql1="select photo from joining WHERE point=(SELECT MAX(point) FROM joining)";
da1=new OleDbDataAdapter(sql1,con);
da1.Fill(ds1,"joining");
string fn,SaveLocation;
int r=0;
DataRow []rs;
rs=ds.Tables["joining"].Select("refno='" + lbl3.Text + "'");
r=rs.Length;
if(r>0)
{
DataRow dr;
dr=rs[0];
lb1.Text=dr["photo"].ToString();
fn=System.IO.Path.GetFileName(lb1.Text);
Image1.ImageUrl=fn;
}
else
{
com.ExecuteNonQuery();
System.Web.UI.WebControls.Label lbl1=new System.Web.UI.WebControls.Label();
lbl1.ForeColor=System.Drawing.Color.Yellow;
lbl1.BackColor=System.Drawing.Color.Blue;
lbl1.Text="Photo Not Available";
ph1.Controls.Add(lbl1);
}
con.Close();
}
Janardan Pandey 6-Feb-12 1:27am    
That's way i am asking wdth u

How to write this control in design time

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