4

我有一张图片需要保存在共享文档下的 Sharepoint 站点中

由于各种原因,图像必须位于 system.drawing.image 中并保存在 sharepoint 站点内的共享文档中。它不必保存在本地硬盘中。

你有什么解决办法吗?

到目前为止,我正在尝试

 MemoryStream ms = new MemoryStream();
 imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
 byte[] imagebyte = ms.ToArray(); 

然后是streamreader的Savebinary功能但没有乐趣

4

2 回答 2

3

过了一会儿,我设法找到了一个避免图片库的解决方案

   void imageSave(System.Drawing.Image imageTobeSaved)
    {
        using (SPSite site = new SPSite("http://sptestsite/"))
        {
            using (SPWeb web = site.RootWeb)
            {
                SPFolder myLibrary = web.Folders["Shared%20Documents"];


                var stream = new System.IO.MemoryStream();
                string filename = "picture.jpg";          
                MemoryStream ms = new MemoryStream();
                imageTobeSaved.Save(ms, System.Drawing.Imaging.ImageFormat.jpg);
                byte[] ImageByte = ms.ToArray();

                SPFile spfile = myLibrary.Files.Add(filename, ImageByte);
                myLibrary.Update();


            }
        }
    }
于 2012-05-22T10:03:25.190 回答
1

十岁的初学者,您需要在此处更改一些硬编码设置

 public void addToSharepointImageList(string folderName, string fileName, byte[] content)
    {
        string path = @"\\yoursite\yourlistname\";
        string baseSharePointPath = "http://yoursite/";
        string listName = "yourlistname"; 
        SharePointImagingService.Imaging svc = null;

        try
        {
            path += folderName;
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            #region create sharepoint service

            svc = new SharePointImagingService.Imaging();

            NetworkCredential nc = new NetworkCredential("username", "password", "domain");
            svc.Credentials = nc;
            //svc.Credentials = System.Net.CredentialCache.DefaultCredentials;
            svc.Url = baseSharePointPath + listName+ "/_vti_bin/imaging.asmx";
            svc.Discover();

            #endregion

            svc.Upload(baseSharePointPath + listName, folderName, content, fileName, true);
        }
        catch (Exception e)
        {
            //deal with error
        }
        finally
        {
            svc.Dispose();
        }

    }

或者,如果您尝试将文件直接保存到非图像列表中,您可以将 sharepoint 视为一个巨大的文件共享,然后打开一个文件流并在那里写入字节数组。

于 2012-05-21T13:34:11.643 回答