0

在客户端对象模型 + 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");
}

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

请帮我定位错误

4

1 回答 1

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 });
    }
于 2014-09-26T07:41:12.023 回答