gpt4 book ai didi

sharepoint - 如何以编程方式从 sharepoint 文档库下载文件

转载 作者:行者123 更新时间:2023-12-04 20:21:00 27 4
gpt4 key购买 nike

在按钮单击事件或链接按钮单击时,我想从 sharepoint 文档库下载文档并将其保存到用户的本地磁盘。

请帮助我,如果您有任何代码示例,请分享

最佳答案

输出文件的直接链接的问题在于,对于某些内容类型,它可能只是在浏览器窗口中打开。如果这不是想要的结果,并且您想强制保存文件对话框,您需要编写一个 ASP/PHP 页面,您可以通过查询字符串将文件名传递给该页面。然后,该页面可以读取文件并在响应中设置一些标题以指示内容处置是和附件。

对于 ASP.net,如果您创建一个名为 download.aspx 的简单 aspx 页面,请将以下代码添加到其中,然后将此文件放在服务器上的某个位置,您可以通过调用此页面来下载文件,如下所示:

http://yourserveraddress/download.aspx?path=http://yoursharepointserver/pathtodoclibrary/file.ext

protected void Page_Load(object sender, EventArgs e)
{
string path = "";
string fileName = "";

path = Request.QueryString["path"];
if (path != null && path.Length > 0)
{
int lastIndex = path.LastIndexOf("/");
fileName = path.Substring(lastIndex + 1, (path.Length - lastIndex - 1));

byte[] data;
data = GetDataFromURL(path);

Response.Clear();
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(data);
Response.Flush();
}
}


protected byte[] GetDataFromURL(string url)
{
WebRequest request = WebRequest.Create(url);
byte[] result;
byte[] buffer = new byte[4096];

//uncomment this line if you need to be authenticated to get to the files on SP
//request.Credentials = new NetworkCredential("username", "password", "domain");

using (WebResponse response = request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (MemoryStream ms = new MemoryStream())
{
int count = 0;
do
{
count = responseStream.Read(buffer, 0, buffer.Length);
ms.Write(buffer, 0, count);
} while (count != 0);
result = ms.ToArray();
}
}
}
return result;
}

关于sharepoint - 如何以编程方式从 sharepoint 文档库下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6608317/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com