Header

Monday, 20 February 2012

How to upload image in Sharepoint Document

Hi Frnds,
Some time we need to upload one image for each pages in document library.
So i am uploading one -one image for each page and giving name for image is page name so it would be unique.
This is being stored in one document library.

  Dim spSite As SPSite = New SPSite("yoursite url")
            Dim _web As SPWeb = spSite.OpenWeb()
            Dim slist As SPList = _web.Lists("Content Images")
            Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
            Dim oInfo As System.IO.FileInfo = New System.IO.FileInfo(sPath)
            Dim sRet As String = HttpUtility.UrlDecode(oInfo.Name)
            Dim imageName As String = sRet.Substring(0, sRet.LastIndexOf("."))
            If FileUploadAttach.HasFile Then
                Dim fileExtension As String = FileUploadAttach.FileName.ToString.Substring(FileUploadAttach.FileName.ToString.LastIndexOf("."))

                imageName = imageName + fileExtension

                Dim destfile As SPFile = slist.RootFolder.Files.Add(slist.RootFolder.Url + "/" + imageName, FileUploadAttach.FileBytes, True)

                Dim item As SPListItem = destfile.Item
                item("Title") = FileUploadAttach.FileName.ToString
                item.Update()
            End If

Thanks and regards
Shailendra Kumar Singh

No comments:

Post a Comment