views:

1691

answers:

2

I would like to add an item into a picture Library using c#. This is how I would add a field to a normal item:

var item = list.Items.Add();
item["Title"] = "Item title";
item.Update();

How would I go about adding the picture? The picture is stored on the file system i.e. c:\myfile.png I iamgine I need to use SPFile but not sure how.

+1  A: 

It is as simple as adding a file to the Document Lib. Below is the code snippet that will help you do it. I have take the code from the this link as the formatting is not proper there I pasted a clean version here

try
        {
            byte[] imageData = null;
            if (flUpload != null)
            {
                // This condition checks whether the user has uploaded a file or not
                if ((flUpload.PostedFile != null) && (flUpload.PostedFile.ContentLength > 0))
                {
                    Stream MyStream = flUpload.PostedFile.InputStream;
                    long iLength = MyStream.Length;
                    imageData = new byte[(int)MyStream.Length];
                    MyStream.Read(imageData, 0, (int)MyStream.Length);
                    MyStream.Close();
                    string filename = System.IO.Path.GetFileName(flUpload.PostedFile.FileName);                                                
                   SPPictureLibrary pic = (SPPictureLibrary)SPContext.Current.Web.Lists["Images"];//Images is the picture library name
                   SPFileCollection filecol = ((SPPictureLibrary)SPContext.Current.Web.Lists["Images"]).RootFolder.Files;//getting all the files which is in pictire library
                   filecol.Add(filename, imageData);//uploading the files to picturte library

                }
            }                
        }
        catch (Exception ex)
        {
            //handle it
        }
Kusek
why the ojSite variable? this is very prone to Dispose errors..
Colin
Well it is a copied code I corrected it thanks
Kusek
+1  A: 

Here's a method to create a byte[] from the file

private byte [] StreamFile(string filename)
{
  FileStream fs = new FileStream(filename, 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));
  //Close the File Stream
  fs.Close();
  return ImageData;
}

// then use the following to add the file to the list
list.RootFolder.Files.Add(fileName, StreamFile(fileName));
Colin
How would I then go about and set the fields on the item?
Rupert
Just need to add a hashtable to the list.RootFolder.Files.Add(fileName, StreamFile(fileName));
Rupert
The Files.Add Method returns an SPFile object. You can then use SPFile.Item to fill in the fields. ie. SPFile file= list.RootFolder.Files.Add(fileName, StreamFile(fileName));SPListItem item = file.item;item["Field" = somevalue;
Colin
Then Call item.Update() when you are done.
Colin