Ads

Tuesday, 16 April 2013

C# Code to Upload File in document library



protected void BtnSaveCauseImage_Click(object sender, EventArgs e)
        {
            if (this.FileUploadCause.PostedFile != null)
            {
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    using (SPSite enterprisesite = new SPSite(this.ArtcleListUrl))
                    {
                        using (SPWeb enterprisesiteweb = enterprisesite.OpenWeb())
                        {
                            SPList ArticleImageslist = enterprisesiteweb.Lists["ArticleImages"];
                            enterprisesite.AllowUnsafeUpdates = true;
                            enterprisesiteweb.AllowUnsafeUpdates = true;
                            SPListItem Items = ArticleImageslist.Items.Add();
                            if (this.FileUploadCause.PostedFile.ContentLength > 0)
                            {
                                int ImgFileSize = this.FileUploadCause.PostedFile.ContentLength;
                                System.IO.Stream strm = this.FileUploadCause.PostedFile.InputStream;

                                byte[] byt = new byte[Convert.ToInt32(this.FileUploadCause.PostedFile.ContentLength)];

                                strm.Read(byt, 0, Convert.ToInt32(this.FileUploadCause.PostedFile.ContentLength));
                                strm.Close();
                                string Imgfilename = System.IO.Path.GetFileName(this.FileUploadCause.PostedFile.FileName);

                                if (Imgfilename.Contains("."))
                                {
                                    string[] delim1 = new string[1];
                                    delim1[0] = ".";
                                    string[] strName = Imgfilename.Split(delim1, StringSplitOptions.RemoveEmptyEntries);
                                    string strFile = strName[0];

                                    strFile = DateTime.Now.ToString() + "" + strFile;
                                    strFile = strFile.Replace("/""-").Replace(":""-").Replace(" """);
                                    Imgfilename = strFile + "." + strName[1];
                                }

                                ArticleImageslist.RootFolder.Files.Add(Imgfilename, byt);

                                string ImagePath = ArtcleListUrl + "ArticleImages/" + Imgfilename;

                                //string strCause = txtCause.Text + "<img src=" + ImagePath + " width=" + "\"100\"" + " " + "height=" + "\"100\">";
                                string strCause = txtCause.Text + "<img src=" + ImagePath + ">";
                                txtCause.Text = strCause;

                            }
                            enterprisesite.AllowUnsafeUpdates = true;
                            enterprisesiteweb.AllowUnsafeUpdates = true;
                        }
                    }
                });
            }
        }

No comments:

Post a Comment

Ads