首页 > Web开发 > 详细

通过webService下载sharepoint文档库文件

时间:2014-09-29 15:22:02      阅读:266      评论:0      收藏:0      [点我收藏+]

第一、基本原理:

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://cntzmsw18: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/

通过webService下载sharepoint文档库文件

原文:http://www.cnblogs.com/yixiaozi/p/4000243.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!