2010-12-08 3 views
1

안녕하세요, Sharepoint 문서 라이브러리에 로컬을 업로드하려고합니다.가상 메모리에 파일을 만듭니다.

다음 코드는 파일을 문서 Libray에 업로드하는 데 적합합니다.

public void UploadFile(string srcUrl, string destUrl) 
    { 
     if (!File.Exists(srcUrl)) 
     { 
      throw new ArgumentException(String.Format("{0} does not exist", 
       srcUrl), "srcUrl"); 
     } 

     SPWeb site = new SPSite(destUrl).OpenWeb(); 

     FileStream fStream = File.OpenRead(srcUrl); 
     byte[] contents = new byte[fStream.Length]; 
     fStream.Read(contents, 0, (int)fStream.Length); 
     fStream.Close(); 

     site.Files.Add(destUrl, contents); 
    } 

하지만 로컬 디스크에 저장하지 않고 "This is a new file"과 같은 콘텐츠가 포함 된 문서 라이브러리에 텍스트 파일을 만들어야합니다.

답변

4

FileStream 대신 MemoryStream을 사용할 수 있습니다. 그런

0

뭔가 :

public void UploadText(string text, Encoding encoding, string destUrl) 
{ 
    SPWeb site = new SPSite(destUrl).OpenWeb(); 
    site.Files.Add(destUrl, encoding.GetBytes(text)); 
} 

PS : 당신이 바이트 배열에 문자열로 변환하는 인코딩을해야합니다. 내가 한 것처럼 하드 코드하거나 매개 변수로 전달할 수 있습니다.

1

문자열을 바이트 배열로 인코딩하고 해당 배열에서 파일을 만들 수 있습니다.

제쳐두고 코드가 SPSiteSPWeb으로 유출되므로 이러한 개체가 많은 메모리를 차지할 수 있으므로 매우 위험합니다. 당신은 그들을 처분해야합니다. 중첩 된 using 진술 :

using System.Text; 

public void AddNewFile(string destUrl) 
{ 
    using (SPSite site = new SPSite(destUrl)) { 
     using (SPWeb web = site.OpenWeb()) { 
      byte[] bytes = Encoding.GetEncoding("UTF-8").GetBytes(
       "This is a new file."); 
      web.Files.Add(destUrl, bytes); 
     } 
    } 
} 
관련 문제