2012-06-30 56 views
0

上傳文件時出錯方法上載在客戶端對象模型+ SharePoint 2010的文件的方法一旦該文件得到了上傳。之後,雖然代碼編譯沒有錯誤錯誤而在客戶端對象模型的SharePoint 2010

在執行我的錯誤

「{」值沒有在預期範圍之內。「}

{} System.Collections.Generic.SynchronizedReadOnlyCollection

我有需要的功能性保健上傳文件

的方法////////////////////////////// ////////////////////////////////////////////////// ///////////

public void Upload_Click(string documentPath, byte[] documentStream) 
{ 
    String sharePointSite = "http://cvgwinbasd003:28838/sites/test04"; 
    String documentLibraryUrl = sharePointSite +"/"+ documentPath.Replace('\\','/'); 

    //////////////////////////////////////////////////////////////////// 
    //Get Document List 
    List documentsList = clientContext.Web.Lists.GetByTitle("Doc1"); 

    var fileCreationInformation = new FileCreationInformation(); 

    //Assign to content byte[] i.e. documentStream 
    fileCreationInformation.Content = documentStream; 

    //Allow owerwrite of document 
    fileCreationInformation.Overwrite = true; 

    //Upload URL 
    fileCreationInformation.Url = documentLibraryUrl; 
    Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(
    fileCreationInformation);    

    //uploadFile.ListItemAllFields.Update(); 
    clientContext.ExecuteQuery(); 
} 

//////////////////////////////////////////// ////////////////////////////////////////////////// ///

在我已經定義下面的方法來調用上傳方法的控制器的MVC 3.0應用程序。

////////////////////////////////////////////// ////////////////////////////////////////////////// //

public ActionResult ProcessSubmit(IEnumerable<HttpPostedFileBase> attachments) 
{ 
    System.IO.Stream uploadFileStream=null; 
    byte[] uploadFileBytes; 
    int fileLength=0;  

    foreach (HttpPostedFileBase fileUpload in attachments) 
    { 
     uploadFileStream = fileUpload.InputStream; 
     fileLength=fileUpload.ContentLength; 
    } 

    uploadFileBytes= new byte[fileLength]; 
    uploadFileStream.Read(uploadFileBytes, 0, fileLength); 

    using (DocManagementService.DocMgmtClient doc = new DocMgmtClient()) 
    { 
     doc.Upload_Click("Doc1/Doc2/Doc2.1/", uploadFileBytes); 
    } 

    return RedirectToAction("SyncUploadResult"); 
} 

///////////////////// ////////////////////////////////////////////////// ///////

請幫我查找錯誤

+0

請添加錯誤調用堆棧或任何其他信息你(的失敗例子高亮線) –

+0

喜,這是拋出錯誤的URL是ploadFileStream.Read(uploadFileBytes, 0,fileLength); – user1481570

+0

試試這個 - http://stackoverflow.com/questions/7852102/convert-httppostedfilebase-to-byte –

回答

0

我覺得你documentLibraryUrl需要是相對的。這是爲我工作與SharePoint 2013

[HttpPost] 
    [ValidateAntiForgeryToken] 
    [SharePointContextFilter] 
    public ActionResult Upload() 
    { 
     if (Request.Files.Count > 0) 
     { 
      HttpPostedFileBase file = Request.Files[0]; 

      if (file != null && file.ContentLength > 0) 
      { 
       var fileName = Path.GetFileName(file.FileName); 

       var spContext = SharePointContextProvider.Current.GetSharePointContext(HttpContext); 

       using (var clientContext = spContext.CreateUserClientContextForSPHost()) 
       { 
        if (clientContext != null) 
        { 
         FileCreationInformation newFile = new FileCreationInformation(); 
         using (MemoryStream ms = new MemoryStream()) 
         { 
          file.InputStream.CopyTo(ms); 
          byte[] array = ms.GetBuffer(); 
          newFile.Content = array; 
         } 
         List docs = clientContext.Web.Lists.GetByTitle("Documents"); 
         Folder folder = docs.RootFolder; 
         clientContext.Load(folder); 
         clientContext.ExecuteQuery(); 
         newFile.Url = docs.RootFolder.ServerRelativeUrl + "/" + fileName; 
         Microsoft.SharePoint.Client.File uploadFile = docs.RootFolder.Files.Add(newFile); 
         clientContext.Load(uploadFile); 
         clientContext.ExecuteQuery(); 

         //Set the metadata 
         Microsoft.SharePoint.Client.ListItem item = uploadFile.ListItemAllFields; 
         string docTitle = string.Empty; 
         item["Title"] = docTitle; 
         item.Update(); 
         clientContext.ExecuteQuery(); 
        } 
       } 
      } 
     } 
     return RedirectToAction("Index", new { SPHostUrl = SharePointContext.GetSPHostUrl(HttpContext.Request).AbsoluteUri }); 
    } 
相關問題