2017-07-17 52 views
0

新手開發這裏的問題,我很感激:-)一些援助圖片上傳/文件從ASP Web應用程序到SharePoint在線

我正在開發一個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 = "[email protected]"; 
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

非常感謝您的回覆InvoiceGuy。不幸的是,這些文件並沒有在那裏顯示。但是,您可能會提出一個有關可能包含某些簽入邏輯的觀點。我會嘗試一下,看看會發生什麼。 –

相關問題