简体   繁体   中英

How to upload file to sharepoint document library

任何人都可以建议我在如何将文件上传到共享点文档库中创建一个银光沙箱解决方案。我是C#开发人员。

You can refer to the following code:

String UploadFile = @"C:\YourFile.txt";
String sharePointWebAdd = "http://yoursite.com/sites/Research/";
String docLib = "Shared Documents";

using (SPSite oSite = new SPSite(sharePointWebAdd))
{
    using (SPWeb oWeb = oSite.OpenWeb())
    {
        if (!System.IO.File.Exists(UploadFile))
            throw new FileNotFoundException("File not found.", UploadFile);                    

        SPFolder myLibrary = oWeb.Folders[docLib];

        // Prepare to upload
        Boolean replaceExistingFiles = true;
        String fileName = System.IO.Path.GetFileName(UploadFile);
        FileStream fileStream = File.OpenRead(UploadFile);

        // Upload document
        SPFile spfile = myLibrary.Files.Add(fileName, fileStream, replaceExistingFiles);

        // Commit 
        myLibrary.Update();
    }
}

Also refer to:

http://msdn.microsoft.com/en-us/library/lists.lists.aspx

little hard to describe over here. but I assume that You will get FileInfo from file. for silver light application you need to add two reference dll of client object model.

Microsoft.SharePoint.Client.Silverlight.dll.
Microsoft.SharePoint.Client.Silverlight.Runtime.dll.

you can find above dll from C:\\Program Files\\Common Files\\Microsoft Shared\\Web Server Extensions\\14\\TEMPLATE\\LAYOUTS\\ClientBin directory path.

 private ClientContext context;
 private Web web;

 private void UploadFile(FileInfo fileToUpload, string libraryTitle, string subfolderPath, bool fileOverwrite)
    {
        try
        {
            //Treatment of files and loading it to byte array []
            Stream str = null;
            Int32 strLen, strRead;

            str = fileToUpload.OpenRead();
            strLen = Convert.ToInt32(str.Length);

            byte[] strArr = new byte[strLen];
            strRead = str.Read(strArr, 0, strLen);

            using (context = new ClientContext("http://localhost/"))
           {
            web = context.Web;

            //Defining where to find the files to tape record the library go
            List destinationList = web.Lists.GetByTitle(libraryTitle);

            //Creating a file
            var fciFileToUpload = new FileCreationInformation();
            fciFileToUpload.Content = strArr;

            //Must determine whether to upload files directly to the library or whether to upload the files to sub directories and stack your way to the file
            string uploadLocation = fileToUpload.Name;

            if (!string.IsNullOrEmpty(subfolderPath))
            {
                uploadLocation = string.Format("{0}/{1}", subfolderPath, uploadLocation);
            }
            uploadLocation = string.Format("{0}/{1}/{2}", webUrl, libraryTitle, uploadLocation);

            //Sets the path to the file where you want to upload and subor whether to overwrite the file or not
            fciFileToUpload.Url = uploadLocation;
            fciFileToUpload.Overwrite = fileOverwrite;

            clFileToUpload = destinationList.RootFolder.Files.Add(fciFileToUpload);

            //load web,list.
            context.Load(web);
            context.Load(destinationList, list => list.ItemCount);
            context.Load(clFileToUpload);
            context.Load(clFileToUpload.ListItemAllFields);
            context.ExecuteQueryAsync(OnLoadingSucceeded, OnLoadingFailed);
           }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            unhideComponents();
        }
    }
    private void OnLoadingSucceeded(Object sender, ClientRequestSucceededEventArgs args)
    {
        Dispatcher.BeginInvoke(fileUploaded); // fileUploaded is function
    }

    private void OnLoadingFailed(object sender, ClientRequestFailedEventArgs args)
    {
        Dispatcher.BeginInvoke(fileNotUploaded); //fileNotUploaded is function
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM