Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
I'm working with the following webpart:
WebPart 1[^]
 
When i click on the save button i'm saving the information to a SharePoint list, and my picture i upload in the fileupload control to a picture library.
 
I want to show the image, and bellow that one instead of the name of the picture i wan't to show the title i'm saving to the List, in this case: ML Crankset.
 
The code i'm using to upload image is:
                    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.Close();
 
                            string fileName =
                                Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
 
                            using (SPWeb myWeb = currentSite.OpenWeb())
                            {
                                SPPictureLibrary pic = (SPPictureLibrary) myWeb.Lists["Product pictures"];
 
                                SPFileCollection fileCol =
                                    ((SPPictureLibrary) myWeb.Lists["Product pictures"]).RootFolder.Files;
                                fileCol.Add(fileName, imageData);
                            }
                        }
                    }
And my whole save button is:
protected void Button_Save_Click(object sender, EventArgs e)
    {
        SPSite currentSite = SPContext.Current.Site;
        SPList myList = currentSite.RootWeb.Lists.TryGetList("Extern Products");
 
        try
        {
            byte[] imageData = null;
 
            if (myList != null)
            {
                if(TextBox_Name.Text == string.Empty &&
                    TextBox_ProdNum.Text == string.Empty &&
                    TextBox_MoreInfo.Text == string.Empty &&
                    TextBox_Color.Text == string.Empty &&
                    TextBox_ListPrice.Text == string.Empty)
 
                {
                    Label_Exception.Text = "Please enter a value.";
                }
                else
                {
                    SPListItem listItem = myList.Items.Add();
 
                    listItem["Title"] = TextBox_Name.Text;
                    listItem["ProductNumber"] = TextBox_ProdNum.Text;
                    listItem["Color"] = TextBox_Color.Text;
                    listItem["ListPrice"] = TextBox_ListPrice.Text;
                    listItem["MoreInformation"] = TextBox_MoreInfo.Text;
 
                    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.Close();
 
                            string fileName =
                                Path.GetFileName(FileUpload_Pic.PostedFile.FileName);
 
                            using (SPWeb myWeb = currentSite.OpenWeb())
                            {
                                SPPictureLibrary pic = (SPPictureLibrary) myWeb.Lists["Product pictures"];
 
                                SPFileCollection fileCol =
                                    ((SPPictureLibrary) myWeb.Lists["Product pictures"]).RootFolder.Files;
                                fileCol.Add(fileName, imageData);
                            }
                        }
                    }
 
                    if (FileUpload_Doc.PostedFile != null && FileUpload_Doc.HasFile)
                    {
                        Stream fStream = FileUpload_Doc.PostedFile.InputStream;
 
                        byte[] contents = new byte[fStream.Length];
                        fStream.Read(contents, 0, (int)fStream.Length);
                        fStream.Close();
                        fStream.Dispose();
 
                        SPAttachmentCollection attachments = listItem.Attachments;
                        string fileName = Path.GetFileName(FileUpload_Doc.PostedFile.FileName);
                        attachments.Add(fileName, contents);
 
                        //listItem["Attached FileName"] = fileName; // store the name of the file in a column for future requirements
                    }
 
                    listItem.Update();
 
                    TextBox_Search.Text = string.Empty;
                    TextBox_Name.Text = string.Empty;
                    TextBox_MoreInfo.Text = string.Empty;
                    TextBox_ProdNum.Text = string.Empty;
                    TextBox_Color.Text = string.Empty;
                    TextBox_ListPrice.Text = string.Empty;
 
                    Label_Exception.Visible = false;
 
                    Dispose();   
                }
            }                   
        }
        catch (Exception x)
        {
            Label_Exception.Text = x.Message;
        }
     }
 
Second question, is it possible to connect the image in picture library with the specific listitem in my SharePoint list? Because i'm working with a second webpart:
WebPart 2[^]
 
Any suggestions? Thanks. /Kristian
Posted 3-Dec-12 23:25pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 CHill60 360
1 Sarvesh Kumar Gupta 238
2 OriginalGriff 193
3 Sergey Alexandrovich Kryukov 163
4 DamithSL 153


Advertise | Privacy | Mobile
Web04 | 2.8.140709.1 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid