Download from Document Library
If you need download a file from document library you can do so easily. First you need to get the SPFile object from the file url. then you need to create a file stream for local file. Finally you need to write the SPFile object to the strem. The following code snippet shows how easy it is
SPFile spFile = currentWeb.GetFile(docLibFileUrl); FileStream outStream = new FileStream(localFileName, FileMode.Create); byte[] fileData = spFile.OpenBinary(); outStream.Write(fileData, 0, fileData.Count()); outStream.Close();
Upload to Document Library
If you ever need to overwrite a file in document library then first you need to get the SPFile. After that you need to checkout the file and finally call the SaveBinary method to save data to the spfile.
SPFile spFile = currentWeb.GetFile(docLibFileUrl);
spFile.CheckOut();
byte[] binaryData=File.ReadAllBytes(localFileName);
spFile.SaveBinary(binaryData, true);
spFile.CheckIn(string.Empty);
FileStream outStream = new FileStream(localFileName, FileMode.Create);
ReplyDeletebyte[] fileData = spFile.OpenBinary();
outStream.Write(fileData, 0, fileData.Count());
write data on hard disk of server, not client.