Monday, October 1, 2012

How to add images/files to document library in sharepoint 2010

In previous post we have seen how to get the updated items from the document library and read the excel file data from document library.Here i will shown how to add the image files to document library/List .For this i have been converted  the attachment file to string format then get the size using some length and memory of this.Then using document  library class the result Data has added to library after check in .
XPathNavigator dX = MainDataSource.CreateNavigator();
XPathNavigator XN = fileAttach.Current;
byte[] converted_attachment = Convert.FromBase64String(XN.ToString());
double filelength= converted_attachment.Length;
double memoryb = 2048 * 2048;
double resultfSize = filelength / memoryb;
if (resultfSize < 8 && resultfSize != 0)
{
int fln = converted_attachement[32] * 2;
byte[] fb = new byte[fln];
for (int i = 0; i < fb.Length; i++)
{
fb[i] = converted_attachement[24 + i];
}
string[] filenameparameter = System.Text.UnicodeEncoding.Unicode.GetChars(fb);
string ResultfName = new string(Filenameparameter);                            
byte[] Result_Data = new byte[converted_attachement.Length - (24 + fln)];
for (int i = 0; i < Result_Data.Length; i++)
{
Result_Data[i] = converted_attachement[24 + fln + i];
}
}
SPDocumentLibrary dl = (SPDocumentLibrary)Web.GetList(Site.ServerRelativeUrl + "/DesiredLibraryName");
SPFolder spfolder = dl.RootFolder;
SPFileCollection spf = spfolder.Files;
SPFile sf = spf.Add(spfolder.Url + "/" + DateTime.Now, Result_Data);
SPListItem spf_item = sf.Item;
spfitem["Filename"] = resultfName;
spfitem.Update();
spfitem.File.CheckIn("WF CheckIn", SPCheckinType.MajorCheckIn);


No comments:

Bel