将大图像文件上载到SharePoint的问题

时间:2011-08-15 22:55:17

标签: c# sharepoint upload sharepoint-clientobject client-object-model

所以我有一个脚本,它基本上遍历一堆分隔的文本文件,并将图像从所述文件上传到SharePoint站点。它工作得很好,期望有一个小问题,我有几个大小> 4MB的图像,当脚本尝试上传它们时会给我(400) Bad Request错误。

以下代码

    class spImageUpload()
    {
        private static System.Collections.Generic.List<string> keywords;
        private static NetworkCredential credentials = new NetworkCredential(username, password, domain);
        private static ClientContext clientContext = new ClientContext(site name);
        private static Web site = clientContext.Web;
        private static List list = site.Lists.GetByTitle(listName);
        private static FileCreationInformation newFile = new FileCreationInformation();

        private static Image image = new Image();
        private static FileIO fo = new FileIO();

        public SharePointAccess()
        {
            sharepointLogin();
            uploadImage();
        }

        private static void updateFields()
        {
            //Loads the site list
            clientContext.Load(list);
            //Creates a ListItemCollection object from list
            ListItemCollection listItems = list.GetItems(CamlQuery.CreateAllItemsQuery());
            //Loads the listItems
            clientContext.Load(listItems);
            //Executes the previous queries on the server
            clientContext.ExecuteQuery();

            //For each listItem...
            foreach (var listItem in listItems)
            {
                //Writes out the item ID and Title
                //Console.WriteLine("Id: {0} Title: {1}", listItem.Id, listItem["Title"]);
                //Loads the files from the listItem
                clientContext.Load(listItem.File);
                //Executes the previous query
                clientContext.ExecuteQuery();
                //Writes out the listItem File Name
                //Console.WriteLine("listItem File Name: {0}", listItem.File.Name);

                //Looks for the most recently uploaded file, if found...
                if (listItem.File.Name.Contains(fileName))
                {
                    title = fileName;
                    //Changes the Title field value
                    listItem["Title"] = title;
                    //Changes the Keywords field value using the keywords list
                    foreach (var keyword in keywords)
                    {
                        listItem["Keywords"] += keyword;
                        //Writes out the item ID, Title, and Keywords
                        //Console.WriteLine("Id: {0} Title: {1} Keywords: {2}", listItem.Id, listItem["Title"], listItem["Keywords"]);
                    }
                }
                //Remember changes...
                listItem.Update();
            }
            //Executes the previous query and ensures changes are committed to the server
            clientContext.ExecuteQuery();
        }

        private static void uploadImage()
        {
            try
            {
                fo.loadFile();

                foreach (var img in fo.lImageSet)
                {
                    Console.WriteLine("Image Name: {0}", img.getName());
                }

                foreach (var img in fo.lImageSet)
                {
                    DateTime start;
                    DateTime end;

                    start = DateTime.Now;
                    //Sets file path equal to the path value stored in the current image of lImageSet
                    filePath = img.getPath();
                    //Writes out to the console indicating what's been stored in filePath
                    Console.WriteLine("Image Path: {0}", filePath);
                    //Reads in the contents of the file
                    newFile.Content = System.IO.File.ReadAllBytes(filePath);
                    //Sets the file name equal to the name value stored in the current image of lImageSet
                    fileName = img.getName() + ".jpeg";
                    //Sets the URL path for the file
                    newFile.Url = fileName;
                    //Creates a List object of type String
                    keywords = new System.Collections.Generic.List<string>();
                    //For each keyword in the current image stored in lImageSet...
                    foreach (var keyword in img.lTags)
                    {
                        //...add that keyword to the newly created list
                        keywords.Add(keyword);
                    }
                    //Uploads the file to the picture library
                    Microsoft.SharePoint.Client.File uploadFile = list.RootFolder.Files.Add(newFile);
                    //Loads uploadFile method
                    clientContext.Load(uploadFile);
                    //Executes previous query
                    clientContext.ExecuteQuery();

                    //Calls the updateFields method to update the associated fields of the most recently uploaded image
                    updateFields();

                    end = DateTime.Now;
                    TimeSpan span = end.Subtract(start);
                    //Writes out to the console to indicate the file has finished being uploaded
                    Console.WriteLine("Uploaded: {0}", fileName + " Done!");
                    Console.WriteLine("Time Elapsed: {0}", span.Seconds + "seconds");

                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }

        private static void sharepointLogin()
        {
            try
            {
                //Loads credentials needed for authentication
                clientContext.Credentials = credentials;
                //Loads the site
                clientContext.Load(site);
                //Loads the site list
                clientContext.Load(list);
                //Executes the previous queries on the server
                clientContext.ExecuteQuery();
                //Writes out the title of the SharePoint site to the console
                Console.WriteLine("Title: {0}", site.Title);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }

现在,我必须使用客户端对象模型远程执行所有操作。我无法使用SharePoint.Administration更改最大上载大小。那么有谁知道如何使用客户端对象模型我可以解决无法上传大于4MB的文件的问题?提前感谢您的帮助!

2 个答案:

答案 0 :(得分:3)

这是因为客户端对象模型的WCF限制。您需要在具有管理员权限的SharePoint管理shell中在服务器上运行此命令:

SPWebService contentService = SPWebService.ContentService;
contentService.ClientRequestServiceSettings.MaxReceivedMessageSize = int.MaxValue;  // 2GB
contentService.Update();

More info here

答案 1 :(得分:2)

尝试使用SaveBinaryDirect方法。 SaveBinaryDirect方法使用基于Web的分布式创作和版本控制(WebDAV)来上载和下载文件。没有构建自己的自定义WCF服务,WebDAV 是上传和下载文件的最有效方式。

using (FileStream lp_fs = new FileStream(is_FileToImport, FileMode.OpenOrCreate))
{
   Microsoft.SharePoint.Client.File.SaveBinaryDirect(lp_context, lp_uri.LocalPath, lp_fs, true);
}
Microsoft.SharePoint.Client.File lp_newFile = lp_web.GetFileByServerRelativeUrl(lp_uri.LocalPath);
lp_context.Load(lp_newFile);
lp_context.ExecuteQuery();

//check out to make sure not to create multiple versions
lp_newFile.CheckOut();

ListItem lp_item = lp_newFile.ListItemAllFields;
listItem["Created"] = info.SourceFile.CreationTime;
listItem["Modified"] = info.SourceFile.LastWriteTime;
listItem.Update();

// use OverwriteCheckIn type to make sure not to create multiple versions 
lp_newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);