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

推荐订阅源

雷峰网
雷峰网
D
Darknet – Hacking Tools, Hacker News & Cyber Security
P
Proofpoint News Feed
Spread Privacy
Spread Privacy
C
Cisco Blogs
L
Lohrmann on Cybersecurity
宝玉的分享
宝玉的分享
I
Intezer
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
The Register - Security
The Register - Security
GbyAI
GbyAI
C
CERT Recently Published Vulnerability Notes
Apple Machine Learning Research
Apple Machine Learning Research
U
Unit 42
Cyberwarzone
Cyberwarzone
爱范儿
爱范儿
I
InfoQ
博客园_首页
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
量子位
P
Palo Alto Networks Blog
Microsoft Azure Blog
Microsoft Azure Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
有赞技术团队
有赞技术团队
T
Tailwind CSS Blog
腾讯CDC
阮一峰的网络日志
阮一峰的网络日志
NISL@THU
NISL@THU
T
Threatpost
T
The Blog of Author Tim Ferriss
云风的 BLOG
云风的 BLOG
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
S
Schneier on Security
Security Latest
Security Latest
Martin Fowler
Martin Fowler
H
Help Net Security
小众软件
小众软件
The Hacker News
The Hacker News
Know Your Adversary
Know Your Adversary
博客园 - 司徒正美
人人都是产品经理
人人都是产品经理
T
The Exploit Database - CXSecurity.com
Jina AI
Jina AI
Engineering at Meta
Engineering at Meta
The GitHub Blog
The GitHub Blog
P
Proofpoint News Feed
IT之家
IT之家
WordPress大学
WordPress大学
S
Securelist

博客园 - 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.