Sunday, June 21, 2009

Uploading images into picture library in sharepoint using c#

Here I explained a sample program to upload image files into picture library

spSite objSite = new SPSite(SPContext.Current.Web.Url);


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);
using (SPWeb myWeb = objSite.OpenWeb())
{
SPPictureLibrary pic = (SPPictureLibrary)myWeb.Lists["Images"];//Images is the picture library name
SPFileCollection filecol = ((SPPictureLibrary)myWeb.Lists["Images"]).RootFolder.Files;//getting all the files which is in pictire library
filecol.Add(filename , imageData);//uploading the files to picturte library
}
}
}
lblMessage.Text = "Uploaded successfully";
}
catch (Exception ex)
{
Response.Write(ex.Message);
}

4 comments:

  1. hi Ramesh Can we upload a image and store that image in picture list of sharepoint default site.
    i had used upload file control of sharepoint. and if yes can i store loaction of that image as url in the list

    ReplyDelete
  2. Hi there,
    I have a problem and want a help;
    To upload an image to a library in sharepoint, I already handle but i don't know how to get it and display it on a webpage..using Object model.
    If you know, pls reponse.
    Thanks.

    ReplyDelete