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),
Inline = true,
};
Response.AppendHeader("Content-Disposition", cd.ToString());
byte[] fileArr=DownloadFile(title, clientContext, filePath,len,extension, "");
Response.AppendHeader("Content-Disposition", cd.ToString());
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)
{
if (itemExtension == ".pdf")
{
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?