【问题标题】:Upload large files 100mb+ to Sharepoint 2010 via c# Web Service通过 c# Web 服务将 100mb+ 的大文件上传到 Sharepoint 2010
【发布时间】:2012-04-24 14:57:58
【问题描述】:

我无法将大文件上传到 Sharepoint 2010。我使用的是 Visual Studio 2010 和语言 C#。我从网上找到的内容中尝试了多种方法,但没有任何效果。我已将设置和配置文件更改为允许的最大上传限制,但仍然没有。我正在将 copy.asmx 用于工作正常的小文件,并且当文件太大并且抛出异常但它不起作用时正在尝试 UploadDataAsync。请看下面的代码...

非常感谢任何/所有帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;

namespace ListsService
{

    public class UploadDocumentcs
    {
        public string UploadResult { get; set; }
        public string Errors { get; set; }
        public UploadDataCompletedEventHandler WebClient_UploadDataCompleted { get; set; }
        public byte[] content { get; set; }

        public void UploadDocumentToSP(string localFile, string remoteFile)
        {
            string result = string.Empty;
            SPCopyService.CopySoapClient client = new SPCopyService.CopySoapClient();

            string sUser = "user";
            string sPwd = "pwd";
            string sDomain = "dmn";
            System.Net.NetworkCredential NC = new System.Net.NetworkCredential(sUser, sPwd, sDomain);

            client.ClientCredentials.Windows.AllowedImpersonationLevel = System.Security.Principal.TokenImpersonationLevel.Impersonation;
            client.ClientCredentials.Windows.ClientCredential = NC;

            try
            {
                client.Open();

                string url = "http://SP/TestLibrary/";
                string fileName = localFile.Substring(localFile.LastIndexOf('\\'), (localFile.Length - localFile.LastIndexOf('\\')));
                fileName = fileName.Remove(0, 1);
                string[] destinationUrl = { url + fileName };

                System.IO.FileStream fileStream = new System.IO.FileStream(localFile, System.IO.FileMode.Open);
                byte[] content = new byte[(int)fileStream.Length];
                fileStream.Read(content, 0, (int)fileStream.Length);
                fileStream.Close();

                // Description Information Field
                SPCopyService.FieldInformation descInfo = new SPCopyService.FieldInformation
                                                  {
                                                      DisplayName = "Description",
                                                      Type = SPCopyService.FieldType.File,
                                                      Value = "Test file for upload"
                                                  };

                SPCopyService.FieldInformation[] fileInfoArray = { descInfo };

                SPCopyService.CopyResult[] arrayOfResults;

                 uint result2 = client.CopyIntoItems(fileName, destinationUrl, fileInfoArray, content, out arrayOfResults);                

                // Check for Errors
                 foreach (SPCopyService.CopyResult copyResult in arrayOfResults)
                 {
                     string msg = "====================================" +
                                  "SharePoint Error:" +
                                  "\nUrl: " + copyResult.DestinationUrl +
                                  "\nError Code: " + copyResult.ErrorCode +
                                  "\nMessage: " + copyResult.ErrorMessage +
                                  "====================================";


                     Errors = string.Format("{0};{1}", Errors, msg);
                 }
                 UploadResult = "File uploaded successfully";

            }
            catch (OutOfMemoryException)
            {
                System.Uri uri = new Uri("http://bis-dev-srv2:300/DNATestLibrary/");
                (new System.Net.WebClient()).UploadDataCompleted += new UploadDataCompletedEventHandler(WebClient_UploadDataCompleted);
                (new System.Net.WebClient()).UploadDataAsync(uri, content);

            }

            finally
            {
                if (client.State == System.ServiceModel.CommunicationState.Faulted)
                {
                    client.Abort();
                    UploadResult = "Upload aborted due to error";
                }

                if (client.State != System.ServiceModel.CommunicationState.Closed)
                {
                    client.Close();
                }
            }
        }


        void WcUpload_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
        {

            if (e != null)
            {
                UploadResult = "Upload Unuccessful";
            }
            else
            {
                UploadResult = "Upload Successful";
                //throw new NotImplementedException();
            }
        }
    }
}

【问题讨论】:

    标签: c# visual-studio-2010 web-services sharepoint file-upload


    【解决方案1】:

    肖恩
    为了使其正常工作,您必须更改 SharePoint 配置以增加上传限制和超时。下面的链接显示了使大文件上传工作的必要步骤。

    http://blogs.technet.com/b/sharepointcomic/archive/2010/02/14/sharepoint-large-file-upload-configuration.aspx

    【讨论】:

    • 谢谢毗湿奴,我已经做到了,但没有帮助。我找到了另一种使用 UploadData 和 UploadDataAsync 的方法,如下所示,这似乎有效。
    【解决方案2】:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net;
    using System.IO;
    namespace UploadTester
    {
        public partial class frmMain : Form
        {
            public frmMain()
            {
                InitializeComponent();
            }
    
            private void btnSelectFile_Click(object sender, EventArgs e)
            {
                openFileDialog1.ShowDialog();
                textBox1.Text = openFileDialog1.FileName;
            }
    
            private void btnUpload_Click(object sender, EventArgs e)
            {
                try
                {
                    byte[] content = GetByteArray();
                    string filename = Path.GetFileName(openFileDialog1.FileName);
    
                    System.Net.WebClient webClient = new System.Net.WebClient();
                    System.Uri uri = new Uri("http://SP/DNATestLibrary/" + filename);
                    webClient.Credentials = new NetworkCredential("username", "pwd", "domain");
    
                    webClient.UploadData(uri, "PUT", content);
    
                    MessageBox.Show("Upload Successful");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    
            byte[] GetByteArray()
            {
                FileStream fileStream = new System.IO.FileStream(openFileDialog1.FileName, System.IO.FileMode.Open);
                byte[] content = new byte[(int)fileStream.Length];
                fileStream.Read(content, 0, (int)fileStream.Length);
                fileStream.Close();
    
                return content;
            }
    
            private void btnUploadAsync_Click(object sender, EventArgs e)
            {
                try
                {
                    byte[] content = GetByteArray();
                    string filename = Path.GetFileName(openFileDialog1.FileName);
    
                    System.Net.WebClient webClient = new System.Net.WebClient();
                    System.Uri uri = new Uri("http://SP/DNATestLibrary/" + filename);
    
                    webClient.UploadDataCompleted += new UploadDataCompletedEventHandler(webClient_UploadDataCompleted);
                    webClient.Credentials = new NetworkCredential("username", "pwd", "domain");
    
                    webClient.UploadDataAsync(uri, "PUT", content);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
    
            void webClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
            {
                if (e.Error == null)
                {
                    MessageBox.Show("Upload Successful");
                }
                else
                {
                    MessageBox.Show(e.ToString());
                }
    
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-04
      • 1970-01-01
      • 1970-01-01
      • 2020-09-22
      • 2010-09-05
      • 1970-01-01
      相关资源
      最近更新 更多