c# - Sharepoint 到 ASP.net MVC 文件下载

标签 c# asp.net asp.net-mvc sharepoint

我很惭愧问这个问题,但不知何故我遗漏了一些东西。

场景

有一个sharepoint实例 Sharepoint 中有一个文档列表,其中包含三个文件 我有一个与 Sharepoint 实例连接的 asp.net MVC 门户 在一个 View 中,它显示了文件列表(在我的例子中是 3 个) 当用户单击该项目时,将下载该文件。

问题

文件已下载,但当您尝试打开它时,提示下载的文件已损坏。

我用谷歌搜索并尝试了所有代码变体。唯一可行的变体是将文件保存在服务器上,然后将其下载到客户端,如您所知这是不可行的

这是我的代码

如上所述,Sharepoint 登录、身份验证等都正常工作 fileref 是文件的共享点路径 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;

        }




    }

我错过了什么?

最佳答案

可能是因为文件大小的确定不正确。尝试从 DownloadFile 方法中删除任何对文件大小的依赖性,如下所示:

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

关于c# - Sharepoint 到 ASP.net MVC 文件下载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32501074/

相关文章:

c# - 使用 ASP.Net Core 中间件启动后台任务

asp.net-mvc - 防止 MVC EF 应用程序中事件重叠的最佳方法是什么?

c# - 带参数的 SqlCommand

c# - 将 UI 定位到鼠标位置(使工具提示面板跟随光标)

c# - MediaComposition.GetThumbnailAsync() 中的内存泄漏?

c# - 向项目中添加 OData 连接服务失败 : Value cannot be null. 参数名称:path1

C# 查找两个字符串之间匹配单词的百分比

c# - 具有空 selectList 参数的 Html.DropDownListFor

asp.net - 两个属性的 CodeFirst 外键

javascript - Web 文本编辑器中的 RTF 格式