Sharepoint 下载 文档库文件(转成字节下载)

由于SharePoint和Office集成得很紧密,所以当用户采取下载副本的时候,会有一个打开按钮,当点击打开的时候可以直接打开,并直接修改文档里的内容,而不经过审批,帮采用自定义的代码来下载,让其不能在线保存,不能直接把File直接Response出去,帮需要把File转成字节输出去,下面是代码的简单示例,不是完善的代码

    SPWeb web = SPContext.Current.Web;

            web.AllowUnsafeUpdates = true;

            SPList list = web.Lists["testDoc"];

            SPListItem item = list.GetItemById(3);

            string strPath = web.Url + "/" + list.RootFolder.Url + "/" + item.Name;

            //Response.Write("strPath:" + strPath + "<br/>");

            SPFile tempFile=web.GetFile(strPath);

            byte[] obj = (byte[])tempFile.OpenBinary();           

            //list.BrowserFileHandling = SPBrowserFileHandling.Permissive;

            Response.ClearContent();

            Response.ClearHeaders();

            Response.AppendHeader("Content-Disposition", "attachment; filename= " + System.Web.HttpUtility.UrlEncode(tempFile.Name));

            Response.ContentType = "application/msword"; 

            Response.BinaryWrite(obj);

            Response.Flush();

            Response.Close(); 

 

你可能感兴趣的:(SharePoint)