Click here to Skip to main content
15,893,668 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How to download files from sharepoint document library using asp.net c#


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.
Posted
Updated 7-Aug-12 19:14pm
v2
Comments
ZurdoDev 7-Aug-12 8:10am    
There is a lot of code to be able to do this. There are samples on msdn. Have you looked there?
Sandeep Mewara 7-Aug-12 11:26am    
Did you try anything?

Oliver -

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

C#
//
// 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);
            }
        }


If your code is located on the SharePoint server you can use the following code (add a reference to the SharePoint.dll):
C#
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;
 
Share this answer
 
v2
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);
 
Share this answer
 
Comments
esb77 6-Nov-13 15:23pm    
tried
WebClient Client=new WebClient();
Client.UseDefaultCredentials=true;
Client.DownloadFile(url, destination);

Worked fine with all kind of files except Doc Library Infopath forms
The output wasn't original file but in a rendered html format(?). Is it possible to download doc. library files

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900