• 通过webService下载sharepoint文档库文件


    第一、基本原理:

    1.通过对象模型得到SPItem.File得到文档库文件

    2.通过WebService将item.File.OpenBinary()返回

    3.将文件保存到服务器

    4.从服务器下载到本地

    第二、具体代码:

    WebService

    [WebMethod]
        public byte[]  GetAttachmentFileflow(string webPath,string list,int fileId) 
        {
            try
            {
                using (SPSite site = new SPSite(webPath))
                {
                    SPWeb web = site.OpenWeb();
                    SPList lists = web.Lists[list];
                    SPListItem item = lists.Items.GetItemById(fileId);
                    return item.File.OpenBinary();
                }
    
            }
            catch (Exception)
            {
                return new byte[] { };
            }
            
        }
    

    保存到服务器,下载的本地

    protected void Page_Load(object sender, EventArgs e)
        {
            File.Delete(path);
            string webPath = "http://18:8099/";
            string list = "Shared Documents";
            localhost.Service server = new localhost.Service();
            byte[] b = server.GetAttachmentFileflow(webPath, list, 1594);//1594为文件ID通过SQL取得
            File.WriteAllBytes(path, b);
        }
    
        protected void BtnClick(object sender, EventArgs e)
        {
            FileStream fileStrem = new FileStream(path, FileMode.Open);
            long fileSize = fileStrem.Length;
            Context.Response.ContentType = "application/octet-stream";//设置报文
            Context.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode("下载.docx", Encoding.UTF8));//下载文件名需要更改
            Context.Response.AddHeader("Content-Length", fileSize.ToString());
            byte[] fileBuffer = new byte[fileSize];
            fileStrem.Read(fileBuffer, 0, (int)fileSize);
            Context.Response.BinaryWrite(fileBuffer);
            fileStrem.Dispose();//释放资源
            Context.Response.End();
        }
    

    第三、源码:

    环境:vs2005 ,sp2007

    注:服务器端对象模型

    http://app.yinxiang.com/l/AAwg2HN1vt9HW78Q29c5fzexeu1Z-DbJh3c/

  • 相关阅读:
    物体也能正常移动
    同时按住两个键
    连续子数组的最大和Java实现
    Entity Framework基础01
    MVC知识进阶01
    面向对象基础进阶03
    面向对象基础进阶02
    面向对象基础进阶01
    little skill---ping
    SqlServer------范式小结
  • 原文地址:https://www.cnblogs.com/yixiaozi/p/4000243.html
Copyright © 2020-2023  润新知