0

I am trying to download a file from Sharepoint on click of a link in ASPX. Facing some issue while writing the bytes to browser, the downloaded image file is partially loaded. when the file type is docx, the downloaded file is corrupt. Below is my code behind. Couldnt solve this though i came across many posts related to this.

using (var ffl = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl))
            {
                byte[] buffer = new byte[8 * 1024];
                ffl.Stream.Read(buffer, 0, buffer.Length);
                Response.Clear();
                Response.ClearHeaders();
                Response.ClearContent();
                Response.Buffer = true;
                Response.AddHeader("Content-Disposition", "attachment;filename=" + file.Name);
                Response.ContentType = "image/JPG";

                Response.BinaryWrite(buffer);
                Response.Flush();
                Response.End();
            }       

1 Answers1

0

Are you sure about the file is JPEG it can be jpg or png. Try using that.

Response.ContentType = "image/JPG"; 
or
Response.ContentType = "image/PNG";
Ravin Gupta
  • 783
  • 7
  • 13