4
我很慚愧地問這個問題,但不知何故我失去了一些東西。Sharepoint到ASP.net MVC文件下載
方案
有一個SharePoint實例 有一個文檔列表在SharePoint中有三個文件 我有一個asp.net MVC門戶網站與SharePoint實例 連接在視圖中它顯示的列表文件(在我的情況下是3) 當用戶點擊該項目時,文件將被下載。
問題
的文件下載,但是當你試圖打開它,話說下載的文件已損壞。
我已經使用它並嘗試了代碼的每個變體。只有工作變化,將文件保存在服務器上,然後將其下載到你知道是不可行的客戶
,這是我的代碼
爲SharePoint登錄以上,認證等提到的所有工作正常 fileref是文件的sharepoint路徑 len從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;
}
}
我錯過了什麼?
瓦迪姆感謝您抽出時間來幫助我。 它像一個魅力伴侶一樣工作。 非常感謝您花時間和精力編寫代碼片段。 – Sudeep