简体   繁体   English

救命! WebClient.UploadFile()在将文件上传到共享点时引发异常

[英]HELP! WebClient.UploadFile() throws exception while uploading files to sharepoint

In my application i am uploading files to sharepoint 2007. I am using 在我的应用程序中,我正在将文件上传到sharepoint 2007。

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

the 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) 在System.Net.WebClient.UploadDataInternal(Uri地址,String方法,Byte []数据,WebRequest和请求)在System.Net.WebClient.UploadData(Uri地址,String方法,Byte []数据)
at System.Net.WebClient.UploadData(String address, String method, Byte[] data) 在System.Net.WebClient.UploadData(字符串地址,字符串方法,Byte []数据)

Any Ideas what I have done wrong? 任何想法我做错了什么?

I am using VS-2008 2.0 我正在使用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. 它来自某个地方的google,但可惜,我是一个顽皮的编码人员,没有在我的评论中添加链接。 Sorry... 抱歉...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM