Uploading files to Sharepoint (WSS 3.0) document l

2019-01-17 08:50发布

问题:

Hi I have the following piece of code to upload a file to Sharepoint. It uses 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."

Does anyone have any ideas as to where I am going wrong?

Thanks,

Alex

回答1:

I've had this issue when I was referencing the url of the document library and not the destination file itself.

i.e. try http://server name/document library name/new file name.doc



回答2:

No clue. But why dont you use Remote Procedure Calls (RPC) thats how i usually do it.

I found this example that might get you started http://geek.hubkey.com/2007/11/upload-file-to-sharepoint-document.html



回答3:

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);
}


回答4:

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.

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.

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.

No, we don't want to use the Sharepoint API because there are no libraries for it in Java. ;-)



回答5:

Is there a paticular reason you can't just use the Sharepoint API (eg. SPFolder.Files.Add) to upload the file? As follows:

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);
}


回答6:

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.

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

This is an exeption. Hopefully it helps.