2010-05-19 81 views
0

在我的應用程序正在將文件上傳到SharePoint 2007我使用幫助! WebClient.UploadFile()拋出異常,同時上傳文件到SharePoint

using (WebClient webClient = new WebClient()) 
{ 
webClient.Credentials = new NetworkCredential(userName, password); 
webClient.Headers.Add("Content-Type", "application/x-vermeer-urlencoded"); 
webClient.Headers.Add("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded"); 
String result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll","POST", data.ToArray())); 
} 

代碼運行successfully..but了一些文件,它拋出異常

基礎連接已關閉: 連接被關閉 意外。在在 System.Net.WebClient.UploadData System.Net.WebClient.UploadDataInternal(URI 地址,字符串方法,字節[]數據, 的WebRequest &請求)(URI 地址,字符串方法,字節[]數據)
在 System.Net.WebClient.UploadData(字符串 地址,字符串的方法,字節[]數據)

我做了什麼錯什麼想法?

我使用VS-2008 2.0

+0

可能問題出在SharePoint服務器上。 – 2010-05-19 19:44:00

回答

1

這裏是我的功能,我用它來上傳文件在同一時間元數據:

public static bool Upload(string webUrl, string documentName, byte[] bytes, Dictionary<string, object> metaInfo, out string result) 
{ 
    string putOption = "overwrite,createdir,migrationsemantics"; // see http://msdn2.microsoft.com/en-us/library/ms455325.aspx 
    string comment = null; 
    bool keepCheckedOut = false; 
    string method = "method=put+document%3a12.0.4518.1016&service_name=%2f&document=[document_name={0};meta_info=[{1}]]&put_option={2}&comment={3}&keep_checked_out={4}\n"; 
    method = String.Format(method, documentName, EncodeMetaInfo(metaInfo), putOption, HttpUtility.UrlEncode(comment), keepCheckedOut.ToString().ToLower()); 
    List<byte> data = new List<byte>(); 
    data.AddRange(Encoding.UTF8.GetBytes(method)); 
    data.AddRange(bytes); 
    try 
    { 
     using (WebClient webClient = new WebClient()) 
     { 
      webClient.Credentials = CredentialCache.DefaultCredentials; 
      webClient.Headers.Add("Content-Type", "application/x-vermeer-urlencoded"); 
      webClient.Headers.Add("X-Vermeer-Content-Type", "application/x-vermeer-urlencoded"); 
      result = Encoding.UTF8.GetString(webClient.UploadData(webUrl + "/_vti_bin/_vti_aut/author.dll", "POST", data.ToArray())); 
      if (result.IndexOf("\n<p>message=successfully") < 0) 
       throw new Exception(result); 
     } 
    } 
    catch (Exception ex) 
    { 
     result = ex.Message; 
     return false; 
    } 

    return true; 
} 

這是從谷歌的地方,但很可惜,我一個頑皮的編碼器,並沒有把我的評論中的鏈接。對不起......

+0

你可以發佈你的EncodeMetaInfo方法嗎? – 2012-01-09 13:33:46

+0

@ThomasStock - 這是相當長的,但我發現我必須借用此代碼的位置,即:http://www.eggheadcafe.com/community/sharepoint/69/10068722/coding-in-windows-service-to-上傳的文檔,在SharePoint的文檔,library.aspx – Reddog 2012-01-10 19:26:17