上传大型图像文件到SharePoint存在问题

3

我有一个脚本,可以遍历一堆分隔的文本文件,并将这些文件中的图像上传到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文件的问题吗?非常感谢您提供的任何帮助!


该限制是一项安全措施,因此我认为如果有可能绕过它,那么这就像是“黑客”方式进入系统。即使可能实现:如果不在服务器端更改限制,则在生产代码中也无法使用。 - Yahia
嗯,所以您认为唯一可行的选项是联系SharePoint管理员,尝试在服务器端更改设置? - This 0ne Pr0grammer
我可能错了,但是这就是我看到的... - Yahia
作为一名前SharePoint管理员,我可以向您保证,唯一的方法是由管理员完成。正如所提到的 - 这是一种安全“功能”。 - EtherDragon
2个回答

3

这是由于WCF客户端对象模型的限制。您需要在具有管理员权限的SharePoint管理Shell中从服务器上运行此操作:

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

更多信息请点击这里


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);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接