如何上传文档到sharepoint 2007 c#
本文关键字:sharepoint 2007 文档 | 更新日期: 2023-09-27 18:12:14
我无法访问Sharepoint服务器,只能像普通用户一样从网页访问。我可以手动上传我的文档。我试图通过c#来解决它,我从net的例子中完成了任何代码。我们的Sharepoint是2007年的。我的代码运行没有任何错误。我把那里的控制文本,看看它是否继续。一切运行正常,但没有发生在Sharepoint页面,没有文件上传。我不知道为什么它什么都不做:)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Net;
namespace Sharepoint
{
class Program
{
public static void CopyStream(Stream read, Stream write)
{
int len; byte[] temp = new byte[1024];
while ((len = read.Read(temp, 0, temp.Length)) > 0)
{
write.Write(temp, 0, len);
/// Console.WriteLine("test");
}
}
static void Main(string[] args)
{
Uri destUri = new Uri("http://gaja/mBreSKCZ/mreports/sales/reportysales/Test_new.txt");
using (FileStream inStream = File.OpenRead(@"C:'Users'TK20382'Test_new.txt"))
{
WebRequest req = WebRequest.Create(destUri);
req.Method = "PUT";
req.Credentials = CredentialCache.DefaultCredentials; // assuming windows Auth
Console.WriteLine("test");
Console.ReadKey();
using (Stream outStream = req.GetRequestStream())
{
CopyStream(inStream, outStream);
}
}
}
}
}
您缺少HttpWebRequest.GetResponse
方法,该方法基本上调用PUT
请求。此外,如果您的目标是。net框架>= 2.0
版本,那么CopyStream
方法可以省略,并且行:
CopyStream(inStream, outStream);
替换为:
inStream.CopyTo(outStream);
public static string UploadFile(string targetUrl,ICredentials credentials, string sourcePath)
{
var request = WebRequest.Create(targetUrl);
request.Method = "PUT";
request.Credentials = credentials;
using (var fileStream = File.OpenRead(sourcePath))
using (var requestStream = request.GetRequestStream())
{
fileStream.CopyTo(requestStream);
}
using (var response = request.GetResponse())
using (var reader = new StreamReader(response.GetResponseStream()))
{
return reader.ReadToEnd();
}
}
使用UploadFile("https://contoso.intranet.com/documents/guide.docx", CredentialCache.DefaultCredentials, @"D:'guide.docx");
也可以使用WebClient.UploadFile
方法,如下所示:
public static void UploadFile(string targeUrl, ICredentials credentials, string fileName)
{
using (var client = new WebClient())
{
client.Credentials = credentials;
client.UploadFile(targeUrl, "PUT", fileName);
}
}