从sharepoint文档库下载文件 [英] download file from sharepoint document library

查看:88
本文介绍了从sharepoint文档库下载文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用asp.net从sharepoint文档库下载文件c#





How to download files from sharepoint document library using asp.net c#


string remoteUri = "http://remote-server name/folder name/";
           string fileName = "parapara2.jpg", myStringWebResource = null;
           // Create a new WebClient instance.
           WebClient myWebClient = new WebClient();
           // Concatenate the domain with the Web resource filename.
           myStringWebResource = remoteUri + fileName;
           Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
           // Download the Web resource and save it into the current filesystem folder.
           myWebClient.DownloadFile(myStringWebResource, fileName);





但这不起作用。



but this is not working.

推荐答案

Oliver -



如果您的代码无法使用SharePoint并且您使用的是ASP.Net,则可以使用以下内容:



Oliver -

If you code cannot use SharePoint and you are using ASP.Net you can use the following:

//
// CopyStream is from 
// http://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file
//
public static void CopyStream(Stream input, Stream output) { 
            byte[] buffer = new byte[8 * 1024]; 
            int len; 
            while ((len = input.Read(buffer, 0, buffer.Length)) > 0) { 
                output.Write(buffer, 0, len); 
            } 
        } 

        protected void Page_Load(object sender, EventArgs e)
        {
            string url = "https://myserver.com/test/Shared%20Documents/mypic.jpg";
            WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
            request.UseDefaultCredentials = true;
            WebResponse response = request.GetResponse();
            Stream fs = response.GetResponseStream() as Stream;
            
            using (FileStream localfs = File.OpenWrite(@"c:\temp\aspdownloadedfile.jpg"))
            {
                CopyStream(fs, localfs);
            }
        }





如果您的代码位于SharePoint服务器上,则可以使用以下代码(添加对SharePoint.dll的引用):



If your code is located on the SharePoint server you can use the following code (add a reference to the SharePoint.dll):

public string GetFileContents(string websiteUrl)
{
            string fileContentString = string.Empty;
            SPSite site = new SPSite(websiteUrl);
            if (site != null)
            {
                SPWeb web = site.OpenWeb();
                if (web != null)
                {
                    fileContentString = web.GetFileAsString(websiteUrl);
                    SPFile file = web.GetFile(websiteUrl);
                    
                }
                else
                {
                    Console.WriteLine("Could not open website {0}", websiteUrl);
                }
                site.Dispose();
            }
}
            return fileContentString;


如果客户帐户已经是您下载前需要使用默认凭据的网站的有效用户,如下所示..



If the client account is already a valid user of the site you just need to use the default credentials before downloading, like so..

WebClient Client=new WebClient();
Client.UseDefaultCredentials=true;
Client.DownloadFile(url, destination);


这篇关于从sharepoint文档库下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆