locked
upload Image on server RRS feed

  • Question

  • User206113050 posted

     

    Hello All,

    I am phasing a problem when i upload an image on server then it shows the below error msg. but it works fine on local.

    A generic error occurred in GDI+.

     

    using below code:

     

     protected void btnUploadNewsImage_Click(object sender, EventArgs e)
            {
                if (fupldNewsImage.PostedFile.ContentLength > 0)
                {
                    if (ViewState["Case"].ToString() != "Edit")
                    {
                        if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["SaveNewsImages"])+ViewState["ImagePath"].ToString()))
                        {
                            File.Delete(Server.MapPath(ConfigurationManager.AppSettings["SaveNewsImages"])+ ViewState["ImagePath"].ToString());
                        }
                        ViewState["ImagePath"] = UploadImage(fupldNewsImage);
                        imgNewsImage.ImageUrl = "~/"+GetNewsImagePath() + ViewState["ImagePath"].ToString();
                        imgNewsImage.Visible = true;
                    }
                    else
                    {
                        if (File.Exists(Server.MapPath(ConfigurationManager.AppSettings["SaveNewsImages"]) + ViewState["ImagePath"].ToString()))
                        {
                            File.Delete(Server.MapPath(ConfigurationManager.AppSettings["SaveNewsImages"]) + ViewState["ImagePath"].ToString());
                        }
                        ViewState["ImagePath"] =UploadImage(fupldNewsImage);
                        imgNewsImage.ImageUrl = "~/"+GetNewsImagePath() + ViewState["ImagePath"].ToString();
                    }
                }         

            }

     

     

    private string UploadImage(FileUpload fileUpload)
            {
                string strFilePath = "";
                try
                {

                    string fullPath = ConfigurationManager.AppSettings["SaveNewsImages"];

                    string tempPath = ConfigurationManager.AppSettings["TempImages"];

                    Random rd = new Random();
                    string s = rd.Next().ToString();
                    strFilePath = fullPath;
                    strFilePath += s + ".jpg";
                    string strTempPath = tempPath + s + ".jpg";
                    fileUpload.PostedFile.SaveAs(Server.MapPath("~/"+strTempPath));
                    System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath("~/"+strTempPath));
                    System.Drawing.Image shortImg = ResizeImage(fullSizeImg,75);
                    string realPath = strFilePath;
                    shortImg.Save(Server.MapPath("~/" + realPath));
                    return s + ".jpg";
                }
                catch (Exception ex)
                {
                    throw ex;
                }
                /*
                 * Return the filename to be stored in the DB
                 */
            }

     

    pls give me suitable response

     

    Thanks.

     

    Tuesday, November 18, 2008 2:07 AM

All replies

  • User1742304165 posted

    I am not sure if this is causing it but you can't use the ~/ when working with the filesystem

     string fullPath = ConfigurationManager.AppSettings["SaveNewsImages"];

                    string tempPath = ConfigurationManager.AppSettings["TempImages"];

                    Random rd = new Random();
                    string s = rd.Next().ToString();
                    strFilePath = fullPath;
                    strFilePath += s + ".jpg";
                    string strTempPath = tempPath + s + ".jpg";
                    fileUpload.PostedFile.SaveAs(Server.MapPath(strTempPath));
                    System.Drawing.Image fullSizeImg = System.Drawing.Image.FromFile(Server.MapPath(strTempPath));

    //Not sure if you have a function Called ResizeImage but i think it is ResizeImageFile(byte[] length, Size), this is probably where your issue is coming from.
                    System.Drawing.Image shortImg = ResizeImage(fullSizeImg,75);

    //no need to to set reaPath p= to strFilePage since i is already in full context.  your just setting a string to another string without changing anything
                    shortImg.Save(Server.MapPath(fullPath));
                    return s + ".jpg"; 

    Hope this helps (question, are you using the fileupload control o just an input type=file html element

    Joshua

    Wednesday, December 10, 2008 1:23 AM