所以我创建了以下脚本:
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网站每15个图像,然后重新登录将工作?提前感谢任何帮助!
可能不是最好的解决方案,但在有人提供更好的解决方案之前,我发现一个临时解决方案就是简单地注销SharePoint并每十分钟重新登录一次....
private static void uploadImage()
{
try
{
foreach (var img in lImageSet)
{
Console.WriteLine("Image Name: {0}", img.getName());
}
foreach (var img in lImageSet)
{
//Counter to track the number of images that have been uploaded
i++;
//For every 10 images that are uploaded, to reduce congestion, log out of SharePoint and log back in.
if (i % 10 == 0)
{
clientContext.Dispose();
sharepointLogin();
}
....