将图像上传到 SharePoint 时如何减少拥塞
所以我创建了以下脚本:
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张图片,然后重新登录可以吗?预先感谢您的任何帮助!
So I have created the following script:
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());
}
}
}
And it works well for the most part. However, on random occasions I will get an error (at a random point in the list of images being uploaded) that "The connection was forcibly closed: a connection that was expected to stay open was closed by the server". I figure this is some sort of congestion issue, and I haven't been able to find many solutions to this problem on Google. So I was wondering if anyone knew of a way to reduce congestion on the server from the client side when uploading image files, or if there was a more efficient way to upload the images, or if a solution like logging out of the SharePoint site every 15 images, then logging back in would work? Thanks in advance for any help!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能不是最好的解决方案,但在有人提供更好的解决方案之前,我发现一个临时解决方案是简单地注销 SharePoint 并每十分钟重新登录一次......
Probably not the best solution, but a temporary resolution I found until someone offers a better one is simply logging out of SharePoint and logging back in every ten minutes....