看到一个添加图片到图片库里的代码,代码如下:

 public string InsertImage()
    {
        try
        {
            if (this.FileUpload3.PostedFile.ContentLength != 0)
            {
                SPWeb web = SPContext.Current.Web;
                web.AllowUnsafeUpdates = true;

                SPList list = web.Lists["照片"];
                SPFolderCollection spfolders = list.RootFolder.SubFolders;
                ArrayList arr = new ArrayList(spfolders.Count);
                //获取上传图片的文件名称(包含后缀)
                string[] imgTemp = FileUpload3.PostedFile.FileName.Split('\\');
                string imgFileName = imgTemp[imgTemp.Length - 1];

                foreach (SPFolder spf in spfolders)
                {
                    arr.Add(spf.Name);
                }
                if (!arr.Contains("Article"))
                {
                    list.RootFolder.SubFolders.Add("Article");
                }
                if (!arr.Contains("Comments"))
                {
                    list.RootFolder.SubFolders.Add("Comments");
                }
                list.RootFolder.SubFolders["Article"].Files.Add(imgFileName, FileUpload3.PostedFile.InputStream, true);//true覆盖原有文件
                web.AllowUnsafeUpdates = false;
                return imgFileName + " 上传成功!#" + web.ServerRelativeUrl + "/" + list.RootFolder.Url + "/" + imgFileName;
            }
            return "#";
        }
        catch (Exception ex)
        {
            this.Label1.InnerText = ex.Message;
            return "#";
        }
    }

 

posted on 2012-07-07 16:40  gzh4455  阅读(439)  评论(1编辑  收藏  举报