Sharepoint to download ASPC MVC file

I am very ashamed to ask this question, but for some reason I am missing something.

Scenario

There is a sharepoint instance. In sharepoint there is a list of documents with three files. I have an asp.net MVC Portal that connects to a Sharepoint instance. In the view, it displays a list of files (3 in my case). When a user clicks on an item, the file must be uploaded.

Problem

The file is loading, but when you try to open it, the word says that the downloaded file is damaged.

I searched for it and tried all the code options. The only variation that works is to save the file to the server and then upload it to the client, which, as you know, is not feasible

this is my code

As mentioned above, Sharepoint login, authentication, etc. everything works correctly fileref - the path to the sharepoint file of the len file is extracted from Sharepoint // int len ​​= int.Parse (oListItemDoc.FieldValues ​​["File_x0020_Size"]. ToString ());

string filePath = fileRef;
        ClientContext clientContext = new ClientContext(GetSharePointUrl());
        clientContext = SharepointAuthorisation(clientContext);
        if (!string.IsNullOrEmpty(filePath))
        {
            var cd = new System.Net.Mime.ContentDisposition
            {
                FileName = Path.GetFileName(fileRef),

                // always prompt the user for downloading, set to true if you want 
                // the browser to try to show the file inline
                Inline = true,
            };
            Response.AppendHeader("Content-Disposition", cd.ToString());
            byte[] fileArr=DownloadFile(title, clientContext, filePath,len,extension, "");
            //FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef.ToString());
            //byte[] arr = new byte[len];
            //fileInfo.Stream.Read(arr, 0, arr.Length - 1);
            //return arr;

            Response.AppendHeader("Content-Disposition", cd.ToString());
            //return new FileStreamResult(fileInfo.Stream,  "application /octet-stream");// vnd.openxmlformats-officedocument.wordprocessingml.document");
            return  File(fileArr, "application/docx" , Path.GetFileName(fileRef));

        }
        else
        {
            return null;
        }



public byte[] DownloadFile(string title, ClientContext clientContext, string fileRef, int len, string itemExtension, string folderName)// Renamed Function Name getdownload to DownloadFiles 
    {
        if (itemExtension == ".pdf")
        {
            //string completePath = Path.Combine(Server.MapPath("~"), folderName);
            //string PdfFile = completePath + "/" + "PDF";
            ////if (!Directory.Exists(PdfFile))
            //{
            //    Directory.CreateDirectory(PdfFile);
            //}

            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef.ToString());
            byte[] arr = new byte[len];
            fileInfo.Stream.Read(arr, 0, arr.Length);
            return arr;
        }
        else
        {

            FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef.ToString());
            byte[] arr = new byte[len];
            fileInfo.Stream.Read(arr, 0, arr.Length);
            return arr;

        }




    }

What am I missing?

+4
source share
1 answer

Perhaps this happens because the file size is determined incorrectly. Try removing any file size dependency from the method DownloadFileas shown below:

public static byte[] DownloadFile(ClientContext ctx,string fileUrl)
{
   var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, fileUrl);
    using (var ms = new MemoryStream())
    {
        fileInfo.Stream.CopyTo(ms);
        return ms.ToArray();
    }
}
+5
source

Source: https://habr.com/ru/post/1606769/


All Articles