C# 如何以编程方式从 sharepoint 站点下载文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/726100/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 22:40:52  来源:igfitidea点击:

How do I programatically download a file from a sharepoint site?

c#filedownload

提问by Scott and the Dev Team

I have a sharepoint site that has an excel spreadsheet that I need to download on a schedulad basis

我有一个 Sharepoint 网站,里面有一个 excel 电子表格,我需要按计划下载

Is this possible?

这可能吗?

采纳答案by aleemb

Why not just use wget.exe <url>. You can put that line in a batch file and run that through windows scheduler.

为什么不直接使用wget.exe <url>. 您可以将该行放在批处理文件中并通过 Windows 调度程序运行它。

回答by mccrager

The link the to document in Sharepoint should be a static URL. Use that URL in whatever solution you have to grab the file on your schedule.

Sharepoint 中指向文档的链接应该是静态 URL。在您必须按计划获取文件的任何解决方案中使用该 URL。

回答by Somebody

You can also do this:

你也可以这样做:

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

回答by MithunRaj

Yes it is possible to download the file from sharepoint. Once you have the url for the document, it can be downloaded using HttpWebRequest and HttpWebResponse.

是的,可以从 sharepoint 下载文件。获得文档的 url 后,就可以使用 HttpWebRequest 和 HttpWebResponse 下载它。

attaching a sample code

附上示例代码

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

    }

You can also use the GetItem API of Copy service to download a file.

您还可以使用 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);

The file can be retrieved as a byte array.

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