C# 将文件上载到Sharepoint上的文件夹或子文件夹
我正在尝试创建一个方法来上传一个文件流到sharepoint到目前为止,我有这个C# 将文件上载到Sharepoint上的文件夹或子文件夹,c#,sharepoint,C#,Sharepoint,我正在尝试创建一个方法来上传一个文件流到sharepoint到目前为止,我有这个 public static void SPUploadFileStream(string username, string filePath, Stream fileData) { //string siteUrl = Configuration.SPSiteURL; string siteUrl = SPContext.Current.Web.Url;
public static void SPUploadFileStream(string username, string filePath, Stream fileData)
{
//string siteUrl = Configuration.SPSiteURL;
string siteUrl = SPContext.Current.Web.Url;
SPUser currentUser = SPUtils.GetCurrentUser(username);
if (currentUser == null)
{
throw new SPGappUnknownUserException(username);
}
using (SPSite site = new SPSite(siteUrl, currentUser.UserToken))
{
using (SPWeb web = site.OpenWeb())
{
bool allowWebUnsafeUpdt = web.AllowUnsafeUpdates;
if (!allowWebUnsafeUpdt)
web.AllowUnsafeUpdates = true;
try
{
SPCreateFolder(Path.GetDirectoryName(filePath), username);
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
}
catch (Exception ex)
{
LoggingService.LogError(ex);
//site.AllowUnsafeUpdates = allowSiteUnsefaUpdt;
web.AllowUnsafeUpdates = allowWebUnsafeUpdt;
throw new ApplicationException("ERROR "+ ex.ToString());
}
}
}
}
但是如果我有一个类似于“FOLDER/file.jpg”
的路径,它可以正常工作,但是当我有子文件夹“FOLDER/SUB/file.jpg”
有人能给我一些建议吗?我猜问题出在您的
SPCreateFolder
方法中。它应该以递归方式创建文件夹。与尝试使用添加新文件时相同
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
服务器相对路径必须存在。尝试以下方法创建文件夹
private static void SPCreateFolder(SPWeb web, string filepath)
{
// since you pass this as Path.GetDictionary it's no longer split by '/'
var foldersTree = filepath.Split('\\');
foldersTree.Aggregate(web.RootFolder, GetOrCreateSPFolder);
}
private static SPFolder GetOrCreateSPFolder(SPFolder sourceFolder, string folderName)
{
SPFolder destination;
try
{
// return the existing SPFolder destination if already exists
destination = sourceFolder.SubFolders[folderName];
}
catch
{
// Create the folder if it can't be found
destination = sourceFolder.SubFolders.Add(folderName);
}
return destination;
}
然后您可以使用
...
SPCreateFolder(web, Path.GetDirectoryName(filePath));
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
...
让我知道这是否有帮助我猜问题出在您的
SPCreateFolder
方法中。它应该以递归方式创建文件夹。与尝试使用添加新文件时相同
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
服务器相对路径必须存在。尝试以下方法创建文件夹
private static void SPCreateFolder(SPWeb web, string filepath)
{
// since you pass this as Path.GetDictionary it's no longer split by '/'
var foldersTree = filepath.Split('\\');
foldersTree.Aggregate(web.RootFolder, GetOrCreateSPFolder);
}
private static SPFolder GetOrCreateSPFolder(SPFolder sourceFolder, string folderName)
{
SPFolder destination;
try
{
// return the existing SPFolder destination if already exists
destination = sourceFolder.SubFolders[folderName];
}
catch
{
// Create the folder if it can't be found
destination = sourceFolder.SubFolders.Add(folderName);
}
return destination;
}
然后您可以使用
...
SPCreateFolder(web, Path.GetDirectoryName(filePath));
SPFile newFile = web.Files.Add(filePath, fileData, true); //true = replace
...
如果有帮助,请告诉我