Upload/Download file to/from WSS document library using DAV

Published 10 April 07 05:58 PM | Rohit Puri 

I recently had to do this in an WPF client application.

Basically this is a static class that provides the following two methods

  • UploadFile - To upload a file to a WSS document library.
  • DownloadFile - To download a file from as WSS document library.
Usage:
DAVHelper.cs

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

using System.Web;

using System.Net;

using System.Data;

using System.Diagnostics;

namespace DAVWrapper

{

public static class DAVHelper

{

public static string UploadFile(string destUrl, string sourcePath)

{

try

{

Uri destUri = new Uri(destUrl);

FileStream inStream = File.OpenRead(sourcePath);

WebRequest req = WebRequest.Create(destUri);

req.Method = "PUT"

req.Headers.Add("Overwrite", "F");

req.Timeout = System.Threading.Timeout.Infinite;

req.Credentials = CredentialCache.DefaultCredentials;

Stream outStream = req.GetRequestStream();

string status = CopyStream(inStream, outStream);

if (status == "success")

{

outStream.Close();

WebResponse ores = req.GetResponse();

return "success"

}

else

{

return status;

}

}

catch (WebException we)

{

return we.Message;

}

catch (System.Exception ee)

{

return ee.Message;

}

}

public static string DownloadFile(string sourceUrl, string destFolder)

{

try

{

System.Uri sourceUri = new System.Uri(sourceUrl);

WebRequest req = WebRequest.Create(sourceUri);

req.Method = "GET"

req.Timeout = System.Threading.Timeout.Infinite;

req.Credentials = CredentialCache.DefaultCredentials;

WebResponse res = req.GetResponse();

Stream inStream = res.GetResponseStream();

FileStream fs = new FileStream(destFolder, FileMode.OpenOrCreate);

string status = CopyStream(inStream, fs);

if (status == "success")

{

inStream.Close();

return "success"

}

else

{

inStream.Close();

return status;

}

}

catch (WebException we)

{

return we.Message;

}

catch (System.Exception ee)

{

return ee.Message;

}

}

private static string CopyStream(Stream inStream, Stream outStream)

{

try

{

byte[] buffer = new byte[1024];

for (; ; )

{

int numBytesRead = inStream.Read(buffer, 0, buffer.Length);

if (numBytesRead <= 0)

break;

outStream.Write(buffer, 0, numBytesRead);

}

inStream.Close();

return "success"

}

catch (System.Exception ee)

{

return ee.Message;

}

}

}

}

Comments

# Joel Oleson's SharePoint Land said on June 27, 2007 4:38 PM:

Something that often comes up in deployments and migrations is what is allowed? What characters are supported.

Anonymous comments are disabled

Search

Go

This Blog

Tags

No tags have been created or used yet.

Syndication

Page view tracker