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

推荐订阅源

L
LangChain Blog
Scott Helme
Scott Helme
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Stack Overflow Blog
Stack Overflow Blog
Forbes - Security
Forbes - Security
T
The Blog of Author Tim Ferriss
Y
Y Combinator Blog
S
Schneier on Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Cisco Talos Blog
Cisco Talos Blog
N
News | PayPal Newsroom
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Project Zero
Project Zero
Cyberwarzone
Cyberwarzone
Vercel News
Vercel News
M
MIT News - Artificial intelligence
云风的 BLOG
云风的 BLOG
Google Online Security Blog
Google Online Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
MongoDB | Blog
MongoDB | Blog
Martin Fowler
Martin Fowler
T
Tenable Blog
I
InfoQ
W
WeLiveSecurity
Google DeepMind News
Google DeepMind News
G
Google Developers Blog
D
DataBreaches.Net
博客园 - Franky
Know Your Adversary
Know Your Adversary
Spread Privacy
Spread Privacy
S
Security @ Cisco Blogs
Hacker News: Ask HN
Hacker News: Ask HN
J
Java Code Geeks
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
F
Full Disclosure
C
Cisco Blogs
Google DeepMind News
Google DeepMind News
P
Proofpoint News Feed
C
CXSECURITY Database RSS Feed - CXSecurity.com
The Cloudflare Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
I
Intezer
The GitHub Blog
The GitHub Blog
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
F
Fortinet All Blogs
Jina AI
Jina AI
V
Visual Studio Blog
L
LINUX DO - 热门话题
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏

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