Obviously, you need to solve this problem, okay, here is the code:
/// <summary>
/// Used for downloading files from DocLib
/// </summary>
public class SPSFileDownloader
{
string m_FileUrl;
string m_FileName;
NetworkCredential m_NetworkCredential;
/// <summary>
/// Constructor
/// </summary>
/// <param name="fileUrl">File URL(http://HostName/sites/LibName/ListName/FileName.doc)</param>
/// <param name="fileName">File Name</param>
/// <param name="networkCredential">Credential</param>
public SPSFileDownloader(string fileUrl, string fileName, NetworkCredential networkCredential)
{
this.m_FileName = fileName;
this.m_FileUrl = fileUrl;
this.m_NetworkCredential = networkCredential;
}
public void Download()
{
HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(this.m_FileUrl);
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add("Translate", "f");
httpWebRequest.Headers = whc;
CredentialCache creCache = new CredentialCache();
creCache.Add( new Uri(this.m_FileUrl), "NTLM", this.m_NetworkCredential);
httpWebRequest.Credentials = creCache;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
long fileLength = httpWebResponse.ContentLength;
string fileName = HttpUtility.UrlPathEncode(this.m_FileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileName);
HttpContext.Current.Response.ContentType ="application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Length", fileLength.ToString());
HttpContext.Current.Response.Buffer = true;
int streamPosition = 1;
byte[] inBuf = new Byte[1024];
while (streamPosition > 0)
{
streamPosition = responseStream.Read(inBuf, 0, inBuf.Length);
HttpContext.Current.Response.OutputStream.Write(inBuf, 0, streamPosition);
HttpContext.Current.Response.Flush();
}
responseStream.Close();
HttpContext.Current.Response.End();
}
}
/// Used for downloading files from DocLib
/// </summary>
public class SPSFileDownloader
{
string m_FileUrl;
string m_FileName;
NetworkCredential m_NetworkCredential;
/// <summary>
/// Constructor
/// </summary>
/// <param name="fileUrl">File URL(http://HostName/sites/LibName/ListName/FileName.doc)</param>
/// <param name="fileName">File Name</param>
/// <param name="networkCredential">Credential</param>
public SPSFileDownloader(string fileUrl, string fileName, NetworkCredential networkCredential)
{
this.m_FileName = fileName;
this.m_FileUrl = fileUrl;
this.m_NetworkCredential = networkCredential;
}
public void Download()
{
HttpWebRequest httpWebRequest = (HttpWebRequest) WebRequest.Create(this.m_FileUrl);
WebHeaderCollection whc = new WebHeaderCollection();
whc.Add("Translate", "f");
httpWebRequest.Headers = whc;
CredentialCache creCache = new CredentialCache();
creCache.Add( new Uri(this.m_FileUrl), "NTLM", this.m_NetworkCredential);
httpWebRequest.Credentials = creCache;
HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse();
Stream responseStream = httpWebResponse.GetResponseStream();
long fileLength = httpWebResponse.ContentLength;
string fileName = HttpUtility.UrlPathEncode(this.m_FileName);
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.AddHeader("content-disposition","attachment;filename="+fileName);
HttpContext.Current.Response.ContentType ="application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Length", fileLength.ToString());
HttpContext.Current.Response.Buffer = true;
int streamPosition = 1;
byte[] inBuf = new Byte[1024];
while (streamPosition > 0)
{
streamPosition = responseStream.Read(inBuf, 0, inBuf.Length);
HttpContext.Current.Response.OutputStream.Write(inBuf, 0, streamPosition);
HttpContext.Current.Response.Flush();
}
responseStream.Close();
HttpContext.Current.Response.End();
}
}
Another approach should be "SPFile.OpenBinary()"
Hope this helps.