使用 c# 从 sharepoint 下载文件到浏览器中的默认下载位置 [英] Download a file from sharepoint using c# to default download location in browser

查看:46
本文介绍了使用 c# 从 sharepoint 下载文件到浏览器中的默认下载位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# 从 sharepoint 站点下载文件.我可以从 sharepoint 站点下载文件,但我们需要指定目标位置.如何在不指定目标路径的情况下将文件下载到浏览器的默认下载位置.

I am trying to download a file from sharepoint site using C#.I am able to download file from sharepoint site but we need to specify the destination location. how can we download the file to default download location of the browser without specifying the destination path.

 var downloadPath = @"D:\out\";
            ClientContext ctx = new ClientContext("https://testsharepoint.sharepoint.com");
            SecureString passWord = new SecureString();
            foreach (char c in "Password12".ToCharArray()) passWord.AppendChar(c);
            ctx.Credentials = new SharePointOnlineCredentials("spuser@ABC.com", passWord);
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, "/sites/Test/Test.docx");
            var fileName = Path.Combine(downloadPath, Path.GetFileName("/sites/Test/Test.docx"));
            using (var fileStream = System.IO.File.Create(fileName))
            {
                fileInfo.Stream.CopyTo(fileStream);
            }

推荐答案

我找到了这个问题的解决方案.我没有将文件流复制到路径,而是从控制器返回文件流.下面是我的代码.

I found solution to this problem. Instead of copying filestream to a path,I return the filestream from the controller.Below is my code.

 [HttpGet]
    public virtual ActionResult Download()
    {          
        ClientContext ctx = new ClientContext("https://ABC.sharepoint.com");
        SecureString passWord = new SecureString();
        foreach (char c in "Pass@word12".ToCharArray()) passWord.AppendChar(c);
        ctx.Credentials = new SharePointOnlineCredentials("abc@gmail12.com", passWord);
        ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
        var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(ctx, "/sites/Test/testdoc.docx");        
        return File(fileInfo.Stream, "application/octet-stream", "testdoc.docx");
    }

这篇关于使用 c# 从 sharepoint 下载文件到浏览器中的默认下载位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆