如何通过编程从SharePoint网站下载文件?

4

我有一个SharePoint网站,其中包含一个Excel电子表格,我需要按照计划定期下载它。

这个可行吗?

4个回答

6

可以从SharePoint下载文件。

一旦获取了文档的URL,可以使用HttpWebRequest和HttpWebResponse进行下载。

以下是示例��码:

    DownLoadDocument(string strURL, string strFileName)
    {
        HttpWebRequest request;
        HttpWebResponse response = null;

            request = (HttpWebRequest)WebRequest.Create(strURL);
            request.Credentials = System.Net.CredentialCache.DefaultCredentials;
            request.Timeout = 10000;
            request.AllowWriteStreamBuffering = false;
            response = (HttpWebResponse)request.GetResponse();
            Stream s = response.GetResponseStream();

            // Write to disk
            if (!Directory.Exists(myDownLoads))
            {
                Directory.CreateDirectory(myDownLoads);
            }
            string aFilePath = myDownLoads + "\\" + strFileName;
            FileStream fs = new FileStream(aFilePath, FileMode.Create);
            byte[] read = new byte[256];
            int count = s.Read(read, 0, read.Length);
            while (count > 0)
            {
                fs.Write(read, 0, count);
                count = s.Read(read, 0, read.Length);
            }

            // Close everything
            fs.Close();
            s.Close();
            response.Close();

    }

您可以使用Copy服务的GetItem API来下载文件。
        string aFileUrl = mySiteUrl + strFileName;
        Copy aCopyService = new Copy();
        aCopyService.UseDefaultCredentials = true;
        byte[] aFileContents = null;
        FieldInformation[] aFieldInfo;
        aCopyService.GetItem(aFileUrl, out aFieldInfo, out aFileContents);

该文件可以作为字节数组检索。


2

你还可以这样做:

try
        {   
            using (WebClient client = new WebClient())
            {
                client.Credentials = new NetworkCredential("username", "password", "DOMAIN");
                client.DownloadFile(http_path, path);                    
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }

0

在Sharepoint中,文件的链接应该是一个静态URL。无论您使用什么解决方案,在您的计划中都要使用该URL来获取文件。


有没有软件可以做到这一点? - Scott and the Dev Team
wget可以实现这个功能。 - mccrager

-1
为什么不直接使用wget.exe <url>呢?你可以将这行代码放入批处理文件中,并通过Windows计划任务运行它。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接