First, the basic principle:
1. Get spitem.file from the object model to get the document library file
2. Return Item.File.OpenBinary () via WebService
3. Save the file to the server
4. Download from server to local
Second, the specific code:
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[] {}; } }
Save to server, download local
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 for file ID through 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";//Set Message Context.Response.AddHeader ("Content-disposition" , "Attachment;filename=" + httputility.urlencode ("download. docx", Encoding.UTF8));//download file name needs to be changed Context.Response.AddHeader ( "Content-length", filesize.tostring ()); byte[] Filebuffer = new Byte[filesize]; Filestrem.read (Filebuffer, 0, (int) fileSize); Context.Response.BinaryWrite (Filebuffer); Filestrem.dispose ();//Release capitalSource Context.Response.End (); }
Third, the source code:
Environment: VS2005, sp2007
Note: Server-side object model
http://app.yinxiang.com/l/AAwg2HN1vt9HW78Q29c5fzexeu1Z-DbJh3c/
Download SharePoint document library files via WebService