HELP! WebClient.UploadFile() throws exception while uploading files to sharepoint
In my application i am uploading files to sharepoint 2007. I am using
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()));
}
开发者_如何学JAVAthe code is running successfully..but for some files it throws exception
The underlying connection was closed: The connection was closed unexpectedly. at System.Net.WebClient.UploadDataInternal(Uri address, String method, Byte[] data, WebRequest& request) at System.Net.WebClient.UploadData(Uri address, String method, Byte[] data)
at System.Net.WebClient.UploadData(String address, String method, Byte[] data)
Any Ideas what I have done wrong?
I am using VS-2008 2.0
Here's my function that I use to upload a document with metadata at the same time:
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;
}
It's from google somewhere, but alas, I'm a naughty coder and didn't put the link in my comments. Sorry...
精彩评论