Click here to Skip to main content
15,900,906 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,


Can anyone help...

how to solve the problems when we upload files in server?

description:


i have one page(Associated college)..it has images...to show this i have to save this images in both folder and database...the problem is it is worked on the local...but when upload it on server it is not work out....how to solve?


code:

HTML
protected void btnnadd_Click(object sender, EventArgs e)
    {
       
        if (uploadcollogo.HasFile == true)
        {
            try
            {
                //string fileName = Server.HtmlEncode(uploadcollogo.FileName);
                    System.Drawing.Image image_file = System.Drawing.Image.FromStream(uploadcollogo.PostedFile.InputStream);
                    int image_height = image_file.Height;
                    int image_width = image_file.Width;
                    int max_height = 240;
                    int max_width = 320;
                    image_height = (image_height * max_width) / image_width;
                    image_width = max_width;
                    if (image_height > max_height)
                    {
                        image_width = (image_width * image_height) / image_width;
                        image_width = max_width;
                    }
                    Bitmap bitmap_file = new Bitmap(image_file, image_width, image_height);
                    System.IO.MemoryStream stream = new System.IO.MemoryStream();
                    bitmap_file.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    stream.Position = 0;
                    byte[] image = new byte[stream.Length + 1];
                    stream.Read(image, 0, image.Length);
                    string fileName = Path.GetFileName(uploadcollogo.FileName);
                   // string image1 = "../userside/collogo/" + fileName;
                    string status = "Active";
                 SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connection"].ConnectionString);
                   // SqlConnection cn = new SqlConnection(ConfigurationManager.AppSettings["constring"].ToString());
                    cn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.CommandText = "pcolinsert";
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@colname", txtcolname.Text);
                    cmd.Parameters.AddWithValue("@colmobile1", txtcolmobile1.Text);
                    cmd.Parameters.AddWithValue("@colmobile2", txtcolmobile2.Text);
                    cmd.Parameters.AddWithValue("@collandline", txtcollandline.Text);
                    cmd.Parameters.AddWithValue("@collemail1", txtcolemail.Text);
                    cmd.Parameters.AddWithValue("@colaltemail", txtcolaltemail.Text);
                    cmd.Parameters.AddWithValue("@coladdress", txtcoladdr.Text);
                    cmd.Parameters.AddWithValue("@colwebsite", txtcolwebsite.Text);
                    cmd.Parameters.AddWithValue("@colcountry", ddlcountry.SelectedItem.Text);
                    cmd.Parameters.AddWithValue("@colstate", ddlstate.SelectedItem.Text);
                    cmd.Parameters.AddWithValue("@colcity", txtcolcity.Text);
                    cmd.Parameters.AddWithValue("@collandmark", txtcollandmark.Text);
                    //cmd.Parameters.AddWithValue("@collogo", image);
                    cmd.Parameters.AddWithValue("@status", status);
                    SqlParameter UploadedImage = new SqlParameter("@collogo", SqlDbType.Image, image.Length);
                    UploadedImage.Value = image;
                    cmd.Parameters.Add(UploadedImage);
                    cmd.Connection = cn;
                    cmd.ExecuteNonQuery();
                    uploadcollogo.SaveAs(Server.MapPath("../userside/collogo/") + uploadcollogo.FileName);
                   // uploadcollogo.SaveAs(Server.MapPath(image1));
                    cn.Close();
                    clear();
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "Inserted", "<script>alert('Record Inserted Sucessfuly')</script>");

                
            }
Posted
Updated 24-Feb-12 20:14pm
v3
Comments
Sudip Saha 24-Feb-12 0:13am    
What is the error you are getting in Server.
sathiyak 24-Feb-12 0:15am    
it has page for insert values...when i am add it is not inserted but also no error came..
Sarvesh Kumar Gupta 24-Feb-12 0:17am    
Please set Response.Write("Step1") at each stage of code. and run this. at which stage, error is coming you will know. then describe what error comes.
Bojjaiah 24-Feb-12 0:24am    
in local application you are getting original path, when you upload server you are getting temp path if you see you can debug the application remotely you can know the whats the error are came. so whats the error post me we can solvethose error.
otherwise you are using internet explorer enable below option.

go throgh internet options=>security tab=>select custom level settings in this iam changing inclde return server path enable.

this is only temp solution you can find another way.
sathiyak 24-Feb-12 0:26am    
Record Not Inserted SuccessfullyCould not find a part of the path 'D:\Hosting\7375777\html\userside\clogo\indexrrr.jpg'.....my friend got this error.....

1 solution

try this

C#
string ServerSavePath = Server.MapPath("~/Upload");
           string path = Path.Combine(ServerSavePath, fileName);
 
Share this answer
 
v2
Comments
sathiyak 24-Feb-12 1:24am    
if anyone know pls give solution....

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