Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm not able to upload And Display Image in My Inline Site.
If i deploy the Site in IIS server the code is running without any problem.
But the images are not able to load on online server.

the code is as under.


C#
try
       {
           Boolean fileOK = false;
           String path = Server.MapPath("~/Assets/");
           String path1 = Server.MapPath("~/Assets/");
           if (txtBIP.HasFile&&txtTIP.HasFile)
           {
               String fileExtension =
                   System.IO.Path.GetExtension(txtBIP.FileName).ToLower();
               String fileExtension1 =
                   System.IO.Path.GetExtension(txtBIP.FileName).ToLower();
               String[] allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
               for (int i = 0; i < allowedExtensions.Length; i++)
               {
                   if (fileExtension == allowedExtensions[i] && fileExtension1 == allowedExtensions[i])
                   {
                       fileOK = true;
                   }
               }
           }
           if (fileOK)
           {
               try
               {
                   txtBIP.PostedFile.SaveAs(path
                       + txtBIP.FileName);
                   txtTIP.PostedFile.SaveAs(path
                       + txtTIP.FileName);
                   Label4.Text = "File uploaded!";
               }
               catch
               {
                   Label4.Text = "File could not be uploaded.";
               }
           }
           else
           {
               Label4.Text = "Cannot accept files of this type.";
           }
           if (txtBIP.HasFile&&txtTIP.HasFile)
           {
               SqlCommand add = new SqlCommand("insert into gallery values('" + ImageId + "','" + "~/Assets/" + txtBIP.FileName.ToString() + "','" + "~/Assets/" + txtTIP.FileName.ToString() + "')", con);
               con.Open();
               add.ExecuteNonQuery();
               con.Close();
               Response.Redirect("~/AddImages.aspx");
           }
           else
           {
               Label4.Text = "Please select a file first";
           }
       }
       catch (Exception er)
       {
           Label4.Visible = true;
           Label4.Text = "Sorry Error in Processing the Request Please Contact System Administrator";
       }
       finally
       {
           con.Close();
       }


please find a proper solution for me

thanks
Posted

<pre lang="xml"><table cellpadding="0" cellspacing="0" border="0">
                                    <asp:UpdatePanel runat="server" ID="imgUpDate">
                                        <ContentTemplate>
                                            <tr>
                                                <td>
                                                    <asp:Image runat="server" ID="imgPhoto" Width="130px" Height="110px" ImageUrl="~/Images/empimg.JPG"
                                                        BorderStyle="Double" />
                                                </td>
                                            </tr>
                                            <tr>
                                                <td>
                                                    <asp:FileUpload runat="server" ID="upbtn" />
                                                    <asp:RegularExpressionValidator runat="server" ID="rgeImage" ControlToValidate="upbtn"
                                                        ErrorMessage="Only (.jpg,.jpeg) Allowed." ValidationExpression="^.*\.(jpg|jpeg|JPG|JPEG)$"></asp:RegularExpressionValidator>
                                                </td>
                                            </tr>
                                        </ContentTemplate>
                                        <Triggers>
                                            <asp:PostBackTrigger ControlID="btnAdd" />
                                        </Triggers>
                                    </asp:UpdatePanel>



wirite this on apsx page.

on cs page.

<pre lang="cs">protected void Insert_Image(int empid)
  {
      try
      {
          if (upbtn.HasFile)
          {
              string img_path = Server.MapPath("~/EmpPhoto\\");
              string extension = Path.GetExtension(upbtn.FileName);
              string imgstr = img_path + empid + extension;

              FileInfo f = new FileInfo(imgstr);
              if (f.Exists)
              {
                  System.IO.File.Delete(imgstr);
                  upbtn.SaveAs(img_path + empid + extension);
              }
              else { upbtn.SaveAs(img_path + empid + extension); }
          }
      }
      catch (Exception ex)
      {

          throw ex;
      }
  }




Now call this function after the success of insert data in table.
 
Share this answer
 
There are two things that I can think of that may not be covered in the context of your code here for image upload.

1) the directory that you are uploading these files to needs to have write privileges which are assigned to the virtual directory in the IIS admin console.

2) The OS folder needs the correct permissions to allow the users identity to write to this folder as well. If you are doing any kind of impersonation you will need some way of adding the identity to the folder Access Control List. If this is not the case then you will need to assign Internet Guest Account permissions.

3) If the file is having issues being displayed there could be issues in your MIME Map you can edit this either with the IIS admin consol or the C:\WINDOWS\system32\inetsrv\MetaBase.xml file.

After all that has been checked Looking through your code, I would strongly recommend if you have a routine to exclude a particular list of file extensions from uploading, you should refactor that into another command that returns a true or false conditions. That is more related to validation not uploading it can help decouple the code cohesion of these two items.

I also noticed

txtBIP.PostedFile.SaveAs(path +txtBIP.FileName);                   txtTIP.PostedFile.SaveAs(path + txtTIP.FileName);<br />


I am not sure what the relevance of this is but BIP is checked TIP is not.

Quick note you should pass parameters into queries using SqlParamaters not inlining code directly.

Finally, I noticed that you are saving the virtual path using ~/Assets/ which is a virtual path that only .NET recognizes. if you extract this into a standard link tag it won't reinterpret into a usable path without using calls to VirtualPathUtility.
 
Share this answer
 
v2

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