简体   繁体   中英

Write byte array to browser

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();
            }       

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";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM