c# - 将大图像文件上传到 SharePoint 时出现问题

标签 c# sharepoint upload sharepoint-clientobject

所以我有一个脚本,它基本上遍历一堆带分隔符的文本文件,并将图像从所述文件上传到 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 的文件的问题?提前感谢您的帮助!

最佳答案

这是因为客户端对象模型的 WCF 限制。您需要从具有管理员权限的 SharePoint 管理外壳在服务器上运行它:

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

More info here

关于c# - 将大图像文件上传到 SharePoint 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7071745/

相关文章:

c# - 使用 Web 服务 API 确定用户在 SharePoint 网站/工作区中的角色

javascript - 如何在不使用 multipart 的情况下使用 AJAX 上传文件?

c# - 在计算日期时间之间的持续时间时安全处理夏令时(或任何其他理论上的非常量偏移)

c# - C#WinForms应用-调试错误-长度不能小于零。参数名称: Length

c# - SPWorkflowActivationProperties 对象引用

javascript - 使用 jQuery 表单插件通过 Ajax 上传文件时不调用错误处理程序

php - 如何在 dropzone.js 中限制最大 "total"文件大小?

c# - 使用 VSTO 在 C# 类文件中访问 Excel 工作表

c# - 同一解决方案中的 WPF 和控制台应用程序 EXE

sharepoint - 如何在 SharePoint 中识别文档