使用c#从sharepoint下载文件到浏览器中的默认下载位置
Download a file from sharepoint using c# to default download location in browser
我正在尝试使用 C# 从 Sharepoint 站点下载文件。我能够从 Sharepoint 站点下载文件,但我们需要指定目标位置。如何在不指定目标路径的情况下将文件下载到浏览器的默认下载位置。
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);
}
我找到了解决这个问题的方法。我没有将文件流复制到路径,而是 return 来自 controller.Below 的文件流是我的代码。
[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 站点下载文件。我能够从 Sharepoint 站点下载文件,但我们需要指定目标位置。如何在不指定目标路径的情况下将文件下载到浏览器的默认下载位置。
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);
}
我找到了解决这个问题的方法。我没有将文件流复制到路径,而是 return 来自 controller.Below 的文件流是我的代码。
[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");
}