通过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/

转载于:https://www.cnblogs.com/yixiaozi/p/4000243.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值