How to upload image in sharepoint attachment of list RRS feed

  • Question

  • Hi,

    i want to upload or add image in attachment column of sharepoint list.on my local pc   (sharepoint 2013 )  its working fine  but on other server machine ( UAT server ) same code is not uploading image .its giving error

    "Could not find file 'Could not find file 'c:\windows\system32\inetsrv\Mehran2.jpg'.."

    although i am loading image from  " d:\working\Mehran2.jpg ".Image is available and as i told you on my local machine its working but from UAT server or same things is not working .

    My Code is:


            private bool AddAttachments2(SPListItem NewItem)
                bool flag = true;

                FileInfo attachment= new FileInfo(fuImages.PostedFile.FileName);

               // {
                    FileStream fs = new FileStream(attachment.FullName, FileMode.Open, FileAccess.Read);
                        // Create a byte array of file stream length
                        byte[] ImageData = new byte[fs.Length];

                        //Read block of bytes from stream into the byte array
                        fs.Read(ImageData, 0, System.Convert.ToInt32(fs.Length));

                        string strpath = System.IO.Path.GetExtension(attachment.Name);
                        if (strpath != ".jpg" && strpath != ".jpeg" && strpath != ".gif" && strpath != ".png")
                            ltrAlert.Text = "Invalid file format only (JPG,Gif and Png) allowed. Record not Saved.";
                            div_message.Style.Add("color", "#FF0000");
                            flag = false;


                        long lngfilesize = fs.Length / 1024;

                        if (lngfilesize > 2048)

                            ltrAlert.Text = "File size must be under 2MB.Record not Saved.";
                            div_message.Style.Add("color", "#FF0000");
                            flag = false;



                        NewItem.Attachments.Add(attachment.Name, ImageData);
                    catch (Exception ex)
                        Helper.LogException("PostAdForm -> AddAttachments2", ex);
                    return flag;



    • Edited by qasimidl Thursday, April 13, 2017 6:35 AM add text
    Thursday, April 13, 2017 6:23 AM

All replies