惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
雷峰网
雷峰网
Hugging Face - Blog
Hugging Face - Blog
IT之家
IT之家
H
Help Net Security
腾讯CDC
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
The GitHub Blog
The GitHub Blog
V
V2EX
M
MIT News - Artificial intelligence
Vercel News
Vercel News
WordPress大学
WordPress大学
博客园 - 三生石上(FineUI控件)
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
阮一峰的网络日志
阮一峰的网络日志
B
Blog RSS Feed
D
Docker
V
Visual Studio Blog
博客园 - 叶小钗
美团技术团队
S
SegmentFault 最新的问题
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com

博客园 - fengzhimei

The template you have chosen is invalid or cannot be found. CSS trick : text-transform Site Definition KickStart Project Transform between Hex and Dec in Javascript - fengzhimei Visual Studio 2005 Web Application Projects Template Overcome limitation of activate ActiveX control in IE "Suggested Web Parts" in SharePoint 2007 Web Part picker page. - fengzhimei Extend toolbar of HtmlEditor in SharePoint 2007 Expand querystring in URL with JavaScript Create a AJAX enabled WebPart for SharePoint2007 by using ASP.NET 2.0 client callback feature Code view is missing in SharePoint Designer Beta 2 when you try to edit a WSS v3 site. MOSS2007(Beta) SDK is online. .Net Framework v2.0 built-in tools StringBuilder in Javascript Remove duplicate rows from a table Remove duplicate items from collection Reset VSS Admin Password Html entity encoder/decoder A nice piece of javascript to simulate Adodb Recordset.
Programatically download file from document library.
fengzhimei · 2005-12-03 · via 博客园 - fengzhimei

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.