sunil mule

sunil mule

  • NA
  • 22
  • 2.1k

Download code not working

Dec 17 2016 1:53 PM
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
 

Answers (1)