从sharepoint文档库下载文件
问题描述:
如何使用asp.net从sharepoint文档库下载文件c#
How to download files from sharepoint document library using asp.net c#
string remoteUri = "http://remote-server name/folder name/";
string fileName = "parapara2.jpg", myStringWebResource = null;
// Create a new WebClient instance.
WebClient myWebClient = new WebClient();
// Concatenate the domain with the Web resource filename.
myStringWebResource = remoteUri + fileName;
Console.WriteLine("Downloading File \"{0}\" from \"{1}\" .......\n\n", fileName, myStringWebResource);
// Download the Web resource and save it into the current filesystem folder.
myWebClient.DownloadFile(myStringWebResource, fileName);
但这不起作用。
but this is not working.
答
Oliver -
如果您的代码无法使用SharePoint并且您使用的是ASP.Net,则可以使用以下内容:
Oliver -
If you code cannot use SharePoint and you are using ASP.Net you can use the following:
//
// CopyStream is from
// http://stackoverflow.com/questions/411592/how-do-i-save-a-stream-to-a-file
//
public static void CopyStream(Stream input, Stream output) {
byte[] buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0) {
output.Write(buffer, 0, len);
}
}
protected void Page_Load(object sender, EventArgs e)
{
string url = "https://myserver.com/test/Shared%20Documents/mypic.jpg";
WebRequest request = WebRequest.Create(new Uri(url, UriKind.Absolute));
request.UseDefaultCredentials = true;
WebResponse response = request.GetResponse();
Stream fs = response.GetResponseStream() as Stream;
using (FileStream localfs = File.OpenWrite(@"c:\temp\aspdownloadedfile.jpg"))
{
CopyStream(fs, localfs);
}
}
如果您的代码位于SharePoint服务器上,则可以使用以下代码(添加对SharePoint.dll的引用):
If your code is located on the SharePoint server you can use the following code (add a reference to the SharePoint.dll):
public string GetFileContents(string websiteUrl)
{
string fileContentString = string.Empty;
SPSite site = new SPSite(websiteUrl);
if (site != null)
{
SPWeb web = site.OpenWeb();
if (web != null)
{
fileContentString = web.GetFileAsString(websiteUrl);
SPFile file = web.GetFile(websiteUrl);
}
else
{
Console.WriteLine("Could not open website {0}", websiteUrl);
}
site.Dispose();
}
}
return fileContentString;
如果客户帐户已经是您下载前需要使用默认凭据的网站的有效用户,如下所示..
If the client account is already a valid user of the site you just need to use the default credentials before downloading, like so..
WebClient Client=new WebClient();
Client.UseDefaultCredentials=true;
Client.DownloadFile(url, destination);