新手开发人员在这里遇到问题我会很感激一些帮助: - )
我正在开发一个ASP Web应用程序(一个简单的公告板,允许用户创建买卖广告)。我的所有数据都是从MSSQL数据库中存储/检索的。但是,我希望使用SharePoint Online库存储/检索广告的图像。
我在第一个障碍中挣扎,即将图像上传到图书馆。我已经调试和调试了一些,但我还在打砖墙。经过几个小时的尝试,我现在有了下面的代码,虽然图像永远不会出现在目标库中,但它不再返回任何错误。
鉴于上述所有情况,我真诚地感谢任何专家的帮助!
以下代码在"创建广告"按下按钮。
string currentUserName =
System.DirectoryServices.AccountManagement.UserPrincipal.Current.DisplayName;
SqlConnection conn2;
SqlCommand comm2;
SqlDataReader reader;
string connectionString2 =
ConfigurationManager.ConnectionStrings["CCString"].ConnectionString;
conn2 = new SqlConnection(connectionString2);
comm2 = new SqlCommand("SELECT TOP 1 * FROM Threads WHERE ([AdvertiserName] = '" + currentUserName + "') ORDER BY PostCreationDateTime DESC", conn2);
conn2.Open();
reader = comm2.ExecuteReader();
reader.Read();
Guid imageID;
string imageIDConfirmed = "";
string fileExtension = "";
if (reader["UniqueImageID"].GetType().Name != "DBNull")
{
imageID = (Guid)reader["UniqueImageID"];
imageIDConfirmed = imageID.ToString().Replace(":", "-");
string myFile = jpgFileUpload.FileName;
fileExtension = myFile.Substring(myFile.Length - 4, 4);
}
reader.Close();
conn2.Close();
string username = "username@emailaddress.com";
string password = "password";
System.Security.SecureString securePass = new
System.Security.SecureString();
foreach (char ch in password.ToCharArray()) securePass.AppendChar(ch);
SharePointOnlineCredentials credentials = new
SharePointOnlineCredentials(username, securePass);
using (ClientContext client = new
ClientContext("https://company.sharepoint.com/sites/SiteName/"))
{
var formLib = client.Web.Lists.GetByTitle("Documents");
client.Credentials = credentials;
client.Load(formLib.RootFolder);
client.ExecuteQuery();
string fileName = @"C:\Temp\" + jpgFileUpload.FileName;
jpgFileUpload.SaveAs(fileName);
var fileUrl = "";
int fileLen;
fileLen = jpgFileUpload.PostedFile.ContentLength;
byte[] input = new byte[fileLen - 1];
input = jpgFileUpload.FileBytes;
UploadDocument(@"https://company.sharepoint.com/sites/SiteName/", "Documents", "https://company.sharepoint.com/sites/SiteName/Shared%20Documents/", "testDocument", input);
using (var fs = new FileStream(fileName, FileMode.Open))
{
var fi = new FileInfo(imageIDConfirmed + fileExtension);
fileUrl = String.Format("{0}/{1}", formLib.RootFolder.ServerRelativeUrl, fi.Name);
Microsoft.SharePoint.Client.File.SaveBinaryDirect(client, fileUrl, fs, true);
client.ExecuteQuery();
}
var libFields = formLib.Fields;
client.Load(libFields);
client.ExecuteQuery();
Microsoft.SharePoint.Client.File newFile =
client.Web.GetFileByServerRelativeUrl(fileUrl);
Microsoft.SharePoint.Client.ListItem item = newFile.ListItemAllFields;
item["Title"] = "Any Title";
item["File Name"] = "Any File Name";
item.Update();
client.Credentials = credentials;
client.ExecuteQuery();
答案 0 :(得分:0)
虽然图像永远不会出现在目标库中,但不会再返回任何错误
文件是否可能存在但处于某种未检查状态?
作为网站集管理员,请转到文档 - >设置 - > "权限和管理"部分 - >管理没有签入版本的文件。
如果有没有签入版本的文件,这可能表示需要在代码中提供签入逻辑,或者可能存在必填字段的问题。