I'm working with a webpart and saving the data to a SPList. I also got two fileUpload controls and i want to upload a picture to a picture library and a document to a document library. Is that possible? And how to? Was googling around but really don't have a clue how to. I also want them to connection with other, for example, if i save the Picture to the librar i want the picture to display it's title, and not the picture url title. enter image description here

This is the code i'm using when i'm uploading the image to a picture library:

        byte[] imageData = null;
        if (fileUpload_Pic != null)
            if ((fileUpload_Pic.PostedFile != null) && (fileUpload_Pic.PostedFile.ContentLength > 0))
                Stream MyStream = fileUpload_Pic.PostedFile.InputStream;
                long iLength = MyStream.Length;
                imageData = new byte[(int)MyStream.Length];
                MyStream.Read(imageData, 0, (int)MyStream.Length);
                string filename = System.IO.Path.GetFileName(fileUpload_Pic.PostedFile.FileName);                                                
               SPPictureLibrary pic = (SPPictureLibrary)SPContext.Current.Web.Lists["Picture library"];//Images is the picture library name
               SPFileCollection filecol = ((SPPictureLibrary)SPContext.Current.Web.Lists["Picture library"]).RootFolder.Files;//getting all the files which is in pictire library
               filecol.Add(filename, imageData);//uploading the files to picturte library


You can do the exact same thing with the document, but instead of using a SPPictureLibrary object, just call add on the Web.Files object and create the path to the library by using the web's url, library name, and the file name you want to appear as in the library.

Or you can do it directly to the library like this SO question.

After you load the file, a SPFile object is returned. You can use that to get the title property and edit the title of the picture, or anything else.

| improve this answer | |
  • Okay thanks. But will the picture be connected with the listitem i add? Like the pic above. – krisal Dec 5 '12 at 13:17
  • By list item do you mean the document added? It could be if you made a content type (Off of Document) that had a lookup column of a picture content type and added the picture's id as that value, or simple record the pictures id as a string value on a custom content type of the document library. – Sam Sussman Dec 5 '12 at 13:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.