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

你可能感兴趣的:(webservice)