Programatically download file from document library.

You probably have this kind of scenario, you have developed a document library system base on the WSS object model, users of this system need to download files from backend WSS site, but sometimes these users can't access the backend WSS site, you just grant the proper permission to them in your system, I mean they are probably the reader to your system, but not the reader to backend WSS site. So if they want to download files, you can't just give them a specific URL(eg. http://HostName/sites/LibName/ListName/FileName.doc").

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();
    }
}

Another approach should be "SPFile. OpenBinary()"

Hope this helps.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值