Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Login
Sign Up
Ask Question
1
Reply
Download code not working
sunil mule
Dec 17 2016 1:53 PM
270
Reply
Hi ,
I have share point visual web part and it contain a button for download some files from share point point lib. but when i did below code it runs smoothly but nothing happens no error no file download i don't know whats wrong with the code can some one please help me to sort it out. below is code sample
string sourceSiteURL=SPContext.Current.Web.Url;
HttpResponse response = HttpContext.Current.Response;
response.Clear();
response.ClearContent();
response.ClearHeaders();
response.Buffer = true;
byte[] obj = null;
using (SPSite oSite = new SPSite(sourceSiteURL))
{
using (SPWeb oWeb = oSite.OpenWeb())
{
oWeb.AllowUnsafeUpdates = true;
SPFolder myLibrary = oWeb.Folders["DocumentSent"];
SPList oList = oWeb.Lists[sourceDocumentLibrary];
SPListItemCollection oListItems = oList.Items;
string line = string.Empty;
try
{
foreach (SPFile oItem in myLibrary.Files)
{
if (oItem.Url.Contains(applicationFileName + ".pdf"))
{
response.AddHeader("Content-Disposition", "attachment;filename=\"" + applicationFileName + ".pdf" + "\"");
response.ContentType = "application/pdf";
StreamReader reader = new StreamReader(oItem.OpenBinaryStream());
SPFile tempFile = oWeb.GetFile(oItem.Url);
obj = (byte[])tempFile.OpenBinary();
if (response.IsClientConnected)
{
response.BinaryWrite(obj);
}
response.BufferOutput = true;
response.Flush();
response.Close();
}
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
//response.End();
}
}
}
Thanks
Upload Source Code
Select only zip and rar file.
Post
Reset
Cancel
Answers (
1
)
Next Recommended Forum
Error occurred in deployment step 'Activate Feature':
SharePoint 2013 online word to Pdf Conversion using CSOM