system.drawing.image到spfile

时间:2012-05-21 13:07:02

标签: c# sharepoint

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

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

你有解决方案吗?

到目前为止,我正在试着

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

然后是streamreader的Savebinary函数但没有欢乐

2 个答案:

答案 0 :(得分: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();


            }
        }
    }

答案 1 :(得分:1)

10的入门者,您需要更改此处的一些硬编码设置

 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视为巨大的文件共享,只需打开文件流并在那里写入字节数组。