C# 使用 HTTP PUT 将文件上传到 Sharepoint (WSS 3.0) 文档库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/596315/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading files to Sharepoint (WSS 3.0) document library using HTTP PUT
提问by taggers
Hi I have the following piece of code to upload a file to Sharepoint. It uses HTTP PUT:
嗨,我有以下代码可以将文件上传到 Sharepoint。它使用 HTTP PUT:
public static string UploadFile(string destUrl, string sourcePath)
{
try
{
Uri destUri = new Uri(destUrl);
FileStream inStream = File.OpenRead(sourcePath);
WebRequest req = WebRequest.Create(destUri);
req.Method = "PUT";
req.Headers.Add("Overwrite", "F");
req.Timeout = System.Threading.Timeout.Infinite;
req.Credentials = CredentialCache.DefaultCredentials;
Stream outStream = req.GetRequestStream();
string status = CopyStream(inStream, outStream);
if (status == "success")
{
outStream.Close();
WebResponse ores = req.GetResponse();
return "success";
}
else
{
return status;
}
}
catch (WebException we)
{
return we.Message;
}
catch (System.Exception ee)
{
return ee.Message;
}
}
When I run this code I get the exception:
当我运行此代码时,出现异常:
"The remote server returned an error: (409) Conflict."
“远程服务器返回错误:(409) 冲突。”
Does anyone have any ideas as to where I am going wrong?
有没有人对我哪里出错有任何想法?
Thanks,
谢谢,
Alex
亚历克斯
采纳答案by nzkarl
I've had this issue when I was referencing the url of the document library and not the destination file itself.
当我引用文档库的 url 而不是目标文件本身时,我遇到了这个问题。
i.e. try http://servername/document library name/new file name.doc
即尝试http://服务器名称/文档库名称/新文件名.doc
回答by Anders Rask
No clue. But why dont you use Remote Procedure Calls (RPC) thats how i usually do it.
没有线索。但是你为什么不使用远程过程调用 (RPC) 这就是我通常这样做的方式。
I found this example that might get you started http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html
我发现这个例子可能会让你开始http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html
回答by mundeep
Is there a paticular reason you can't just use the Sharepoint API (eg. SPFolder.Files.Add) to upload the file? As follows:
是否有特殊原因不能仅使用 Sharepoint API(例如 SPFolder.Files.Add)上传文件?如下:
http://msdn.microsoft.com/en-us/library/ms454491.aspx
http://msdn.microsoft.com/en-us/library/ms454491.aspx
public void UploadFile(string srcUrl, string destUrl)
{
if (! File.Exists(srcUrl))
{
throw new ArgumentException(String.Format("{0} does not exist",
srcUrl), "srcUrl");
}
SPWeb site = new SPSite(destUrl).OpenWeb();
FileStream fStream = File.OpenRead(srcUrl);
byte[] contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
fStream.Close();
EnsureParentFolder(site, destUrl);
site.Files.Add(destUrl, contents);
}
回答by tata9999
Alex, This happened to me too. You probable should create another another lit or document library and upload files into it to test.
亚历克斯,这也发生在我身上。您可能应该创建另一个照明或文档库并将文件上传到其中进行测试。
You may want to check the variable "destUri" to see if it points to exactly the expected sharepoint list.
您可能需要检查变量“destUri”以查看它是否正好指向预期的共享点列表。
My situation is I firstly created a document library "Requrements", there is a typo mistake, then i changed the title to "Requirements". You should notice that sharepoint still keeps the URL to this list as http://server:port/Requrements
我的情况是我首先创建了一个文档库“要求”,有一个错字错误,然后我将标题更改为“要求”。您应该注意到,sharepoint 仍然保留此列表的 URL 为http://server:port/Requrements
This is an exeption. Hopefully it helps.
这是一个例外。希望它有帮助。
回答by Marco Dorantes
Try:
尝试:
void StorePlainFile(string target_url, string filename, byte[] file_bytes)
{
string url = target_url + "/" + filename;
System.Net.WebClient client = new System.Net.WebClient();
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
client.Headers.Add("Overwrite", "F");
byte[] response = client.UploadData(url, "PUT", file_bytes);
}
回答by Andrew Jennings
I haven't solved my problem yet, that's why I'm here, but I know why you're getting this error.
我还没有解决我的问题,这就是我在这里的原因,但我知道你为什么会收到这个错误。
The error results because you are not setting a hidden, but required, field. In my case, I had no columns, and certainly none that were required. However, there is a versioning field that is in conflict.
导致错误的原因是您没有设置隐藏但必需的字段。就我而言,我没有列,当然也不需要列。但是,存在冲突的版本控制字段。
My intent is to 1) upload the document, and 2) set the document's metadata. 1) and 2) occur over separate HTTP calls. Ideally, I want to do this in a single call, but I don't know how to do this.
我的意图是 1) 上传文档,以及 2) 设置文档的元数据。1) 和 2) 发生在单独的 HTTP 调用上。理想情况下,我想在一次调用中完成此操作,但我不知道如何执行此操作。
To accomplish this, 1) succeeds, so the document appears in the library. Then when I try to update the metadata, that's when I get the 409 error.
要完成此操作,1) 成功,因此该文档出现在库中。然后,当我尝试更新元数据时,就会收到 409 错误。
I'm pretty sure that I first need to insert a step in between 1) and 2) which first downloads the document's list (or manifest) which would in theory contain the needed versioning information. All I would need to do is set the metadata fields I need, and send back to the server.
我很确定我首先需要在 1) 和 2) 之间插入一个步骤,该步骤首先下载文档列表(或清单),理论上该列表将包含所需的版本信息。我需要做的就是设置我需要的元数据字段,然后发送回服务器。
No, we don't want to use the Sharepoint API because there are no libraries for it in Java. ;-)
不,我们不想使用 Sharepoint API,因为 Java 中没有它的库。;-)