Sharepoint 将项目添加到图片库中

发布于 2024-08-03 06:51:13 字数 212 浏览 2 评论 0原文

我想使用 c# 将项目添加到图片库中。这就是我向普通项目添加字段的方式:

var item = list.Items.Add();
item["Title"] = "Item title";
item.Update();

我将如何添加图片?图片存储在文件系统上,即 c:\myfile.png 我想我需要使用 SPFile 但不知道如何使用。

I would like to add an item into a picture Library using c#. This is how I would add a field to a normal item:

var item = list.Items.Add();
item["Title"] = "Item title";
item.Update();

How would I go about adding the picture? The picture is stored on the file system i.e. c:\myfile.png I iamgine I need to use SPFile but not sure how.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

咿呀咿呀哟 2024-08-10 06:51:13

这是从文件创建 byte[] 的方法

private byte [] StreamFile(string filename)
{
  FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
  // Create a byte array of file stream length
  byte[] ImageData = new byte[fs.Length];
  //Read  block of bytes from stream into the byte array
  fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));
  //Close the File Stream
  fs.Close();
  return ImageData;
}

// then use the following to add the file to the list
list.RootFolder.Files.Add(fileName, StreamFile(fileName));

Here's a method to create a byte[] from the file

private byte [] StreamFile(string filename)
{
  FileStream fs = new FileStream(filename, FileMode.Open,FileAccess.Read);
  // Create a byte array of file stream length
  byte[] ImageData = new byte[fs.Length];
  //Read  block of bytes from stream into the byte array
  fs.Read(ImageData,0,System.Convert.ToInt32(fs.Length));
  //Close the File Stream
  fs.Close();
  return ImageData;
}

// then use the following to add the file to the list
list.RootFolder.Files.Add(fileName, StreamFile(fileName));
黯淡〆 2024-08-10 06:51:13

就像将文件添加到文档库一样简单。下面是可以帮助您完成此操作的代码片段。我已从此链接 由于格式不正确,我在这里粘贴了一个干净的版本

try
        {
            byte[] imageData = null;
            if (flUpload != null)
            {
                // This condition checks whether the user has uploaded a file or not
                if ((flUpload.PostedFile != null) && (flUpload.PostedFile.ContentLength > 0))
                {
                   Stream MyStream = flUpload.PostedFile.InputStream;
                   long iLength = MyStream.Length;
                   imageData = new byte[(int)MyStream.Length];
                   MyStream.Read(imageData, 0, (int)MyStream.Length);
                   MyStream.Close();
                   string filename = System.IO.Path.GetFileName(flUpload.PostedFile.FileName);                                                
                   SPPictureLibrary pic = (SPPictureLibrary)SPContext.Current.Web.Lists["Images"];//Images is the picture library name
                   SPFileCollection filecol = ((SPPictureLibrary)SPContext.Current.Web.Lists["Images"]).RootFolder.Files;//getting all the files which is in picture library
                   filecol.Add(filename, imageData);//uploading the files to picture library
                    
                }
            }                
        }
        catch (Exception ex)
        {
            //handle it
        } 

It is as simple as adding a file to the Document Lib. Below is the code snippet that will help you do it. I have take the code from the this link as the formatting is not proper there I pasted a clean version here

try
        {
            byte[] imageData = null;
            if (flUpload != null)
            {
                // This condition checks whether the user has uploaded a file or not
                if ((flUpload.PostedFile != null) && (flUpload.PostedFile.ContentLength > 0))
                {
                   Stream MyStream = flUpload.PostedFile.InputStream;
                   long iLength = MyStream.Length;
                   imageData = new byte[(int)MyStream.Length];
                   MyStream.Read(imageData, 0, (int)MyStream.Length);
                   MyStream.Close();
                   string filename = System.IO.Path.GetFileName(flUpload.PostedFile.FileName);                                                
                   SPPictureLibrary pic = (SPPictureLibrary)SPContext.Current.Web.Lists["Images"];//Images is the picture library name
                   SPFileCollection filecol = ((SPPictureLibrary)SPContext.Current.Web.Lists["Images"]).RootFolder.Files;//getting all the files which is in picture library
                   filecol.Add(filename, imageData);//uploading the files to picture library
                    
                }
            }                
        }
        catch (Exception ex)
        {
            //handle it
        } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文