Programatically download file from document library.

Source: Internet
Author: User
Tags file url
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 ").

Obviusly, 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)
{
This . M_filename = Filename;
This . M_fileurl = Fileurl;
This . M_networkcredential = Networkcredential;
}

Public   Void Download ()
{
Httpwebrequest = (Httpwebrequest) webrequest. Create ( This . M_fileurl );
Webheadercollection WHC =   New Webheadercollection ();
WHC. Add ( " Translate " , " F " );
Httpwebrequest. Headers = WHC;

Credentialcache crecache= NewCredentialcache ();
Crecache. Add (NewUri (This. M_fileurl ),"NTLM",This. M_networkcredential );

Httpwebrequest. Credentials=Crecache;

httpwebresponse = (httpwebresponse) httpwebrequest. getresponse ();

Stream responsestream=Httpwebresponse. getresponsestream ();
LongFilelength=Httpwebresponse. contentlength;
StringFilename=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 shoshould be "spfile. openbinary ()"

Hope this helps.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.