Click here to Skip to main content
16,015,481 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi,
How do you download a file from a document library in sharepoint to local folder? i am currently making a Windows application using C# that will download documents from a document library list.
i have tried this code. but i got error in
using (Stream destFile = System.IO.File.OpenWrite(destPath)) this line.
C#
public void Download()
       {
           string destPath=@"c:/Folder";
           Sp.ClientContext spcontext = new ClientContext("http://Sharepointsite");
           Directory.CreateDirectory(destPath);
           using (FileInformation ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(spcontext, "/Image"))
           {
               using (Stream destFile = System.IO.File.OpenWrite(destPath))
               {
                   byte[] buffer = new byte[8 * 1024];
                   int len;
                   while ((len = ffl.Stream.Read(buffer, 0, buffer.Length)) > 0)
                   {
                       destFile.Write(buffer, 0, len);
                   }
               }
           }
       }
Posted
Updated 19-Jul-13 2:23am
v3
Comments
[no name] 19-Jul-13 8:18am    
www.whathaveyoutried.com. A search through bing for "download a file from a document library in sharepoint to local folder" would give you 11,000,000 results so what have you tried? Where is the code that demonstrates your problem? Any problem?
[no name] 19-Jul-13 9:30am    
Without knowing what "i got error" actually means, I would have to guess that "c:/Folder" is not a valid filename that you are trying to pass to the OpenWrite method.

 
Share this answer
 
v2
 Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic
' The ScriptMain class is the entry point of the Script Task.

Imports System.Net
Imports System.IO
Imports System
Imports System.Data
Imports System.Math
Imports System.Web
Imports System.Object
Imports System.Web.HttpResponse
Imports Microsoft.SqlServer.Dts.Runtime

Public Class ScriptMain

    Public Sub Main()
        ' Dim strFileName As String = System.IO.Path.GetRandomFileName()
        Dim URL As String = Dts.Variables("Url").Value.ToString()
        Dim strFriendlyName As String = Dts.Variables("Filename").Value.ToString() '"sample.txt"

        Dim dir As DirectoryInfo = New DirectoryInfo(Dts.Variables("ImportFolder").Value.ToString())

        Dim credentials As System.Net.NetworkCredential = System.Net.CredentialCache.DefaultNetworkCredentials
        Dim source As New Uri("https://testportal-ab.abc.net/deps/test/fold/destfold/sample.txt")
        ' Dim source1 As New Uri(URL & "/" & strFriendlyName & fileExtension)

        Dim destination As String = "C:\Users\users1\Desktop\destfold\" & strFriendlyName
        My.Computer.Network.DownloadFile(source, destination, credentials, True, 60000I, False)
    End Sub

End Class
 
Share this answer
 

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