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

推荐订阅源

The GitHub Blog
The GitHub Blog
V2EX - 技术
V2EX - 技术
T
Threat Research - Cisco Blogs
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
T
Tor Project blog
Project Zero
Project Zero
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
C
Cybersecurity and Infrastructure Security Agency CISA
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
AWS News Blog
AWS News Blog
Scott Helme
Scott Helme
C
Cisco Blogs
Application and Cybersecurity Blog
Application and Cybersecurity Blog
O
OpenAI News
P
Privacy International News Feed
Google Online Security Blog
Google Online Security Blog
SecWiki News
SecWiki News
The Last Watchdog
The Last Watchdog
NISL@THU
NISL@THU
Attack and Defense Labs
Attack and Defense Labs
G
GRAHAM CLULEY
Security Latest
Security Latest
Help Net Security
Help Net Security
C
Cyber Attacks, Cyber Crime and Cyber Security
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
A
Arctic Wolf
Security Archives - TechRepublic
Security Archives - TechRepublic
S
Security @ Cisco Blogs
腾讯CDC
S
Secure Thoughts
WordPress大学
WordPress大学
P
Proofpoint News Feed
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
小众软件
小众软件
M
MIT News - Artificial intelligence
博客园 - 叶小钗
IT之家
IT之家
G
Google Developers Blog
博客园 - 聂微东
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
Hacker News - Newest:
Hacker News - Newest: "LLM"
Google DeepMind News
Google DeepMind News
N
News and Events Feed by Topic
N
News and Events Feed by Topic

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