2012-05-21 33 views
4

我有我需要一個SharePoint站點保存在共享文檔爲System.Drawing.Image到SPFILE

由於各種原因,圖像必須在System.Drawing.Image對象和被保存在一個圖像在SharePoint站點內共享文檔。它不必保存在本地硬盤上。

你有什麼解決方案嗎?

到目前爲止我gtrying

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

中的StreamReader的那麼Savebinary功能,但沒有喜悅

回答

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視爲巨大的文件共享,只需打開文件流並寫入字節數組t這裏。

+1

是的,我一直在尋找更多的差不多了,我需要因爲SharePoint使用WebDAV的,你可以簡單地將文件寫入到文件命名爲 – user1211929

+0

的最後一個選項到\\ sharepointserver \共享文件。 –

+0

@brianbrinley,真的嗎?基金會2010對我來說工作得很好......坦率地說,你不能使用直接寫入方法來設置任何附加屬性,它們都被設置爲默認值,但文件進入並且可訪問就好了。就像在資源管理器中打開列表並拖放一樣。 – Stuart

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(); 


      } 
     } 
    } 
相關問題