从ASP上传文档.Net到Sharepoint服务器
本文关键字:Sharepoint 服务器 Net 文档 ASP | 更新日期: 2023-09-27 17:51:02
我正在尝试从ASP上传文件。Net到Sharepoint(为了保留时间戳,我使用这种方式)
下面是我的代码 protected void UploadFileToSharePoint(string UploadedFilePath, string SharePointPath)
{
WebResponse response = null;
try
{
string SUrl = "http://MysharepointPath/Folder";
//WebRequest request = WebRequest.Create(SharePointPath);
WebRequest request = WebRequest.Create(SUrl);
request.Credentials = new NetworkCredential(username,password );
//request.Method = "PUT";
request.Method = "POST";
FileStream fStream = File.OpenRead(UploadedFilePath);
string fileName = fStream.Name.Substring(3);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
request.ContentLength = 0;
//Custom code
using (WebClient uploader = new WebClient())
{
try
{
uploader.UploadFile(new Uri(SUrl), UploadedFilePath);
}
catch (Exception ex)
{
}
}
response = request.GetResponse();
}
}
当我尝试在调试模式下运行代码时,它抛出异常
"远程服务器返回错误401 unauthorized"
你说的'(为了保存时间戳,我用这种方式)'是什么意思?在SharePoint中上传文档的时间戳将是文档被添加到SharePoint文档库的日期
错误401(未授权),表明客户端必须首先验证自己,所以你可以使用Fiddler来验证你是否在SharePoint服务器上进行身份验证吗?
您使用的是OnPremises还是SharePoint Online ?
在codeplex中有一些示例可以帮助你完成,请查看这个链接以获取更多信息…http://spfileupload.codeplex.com/SourceControl/latest Get-SPScripts.Copy-FilesToSP.ps1