使用sharepoint rest api c#httpwebrequest下载文档 [英] Download the document using sharepoint rest api c# httpwebrequest

查看:88
本文介绍了使用sharepoint rest api c#httpwebrequest下载文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下端点


url:http:// site url / _api / web / GetFolderByServerRelativeUrl('/ Folder Name')/ Files('file name ')/ $ value

方法:GET


如果我在浏览器中浏览端点,它工作正常。当我尝试使用httpwebrequest和getresponsestream()函数时,它不会返回文件流。


请做必要的

解决方案


以下代码片段供您参考。

 var siteURL =" http:// sp2013 / sites / team" ;; 
var username =" dennis" ;;
var password =" password" ;;
var domain =" contoso" ;;

var folderRelativeUrl =" / sites / team / DL" ;;
var fileName =" Test.pdf" ;;

HttpWebRequest request = null;
HttpWebResponse response = null;
string commandString = string.Empty;

commandString = string.Format(" / _ api / web / GetFolderByServerRelativeUrl('{0}')/ files('{1}')/


值",folderRelativeUrl,文件名);
Uri uri = new Uri(siteURL + commandString);

//设置HTTP请求
请求=(HttpWebRequest)WebRequest.Create(uri);
request.Credentials = new NetworkCredential(用户名,密码,域名);
request.Method = WebRequestMethods.Http.Get;

response =(HttpWebResponse)request.GetResponse();
if(response.ContentLength> 0)response.ContentLength = response.ContentLength;
Stream output = response.GetResponseStream();
using(var fs = new FileStream(" c:\\temp \\" + fileName,FileMode.OpenOrCreate))
output.CopyTo(fs);

更多信息:


https://social.technet.microsoft.com/论坛/共享点/ EN-US / f6b1f3c2-d8e5-4d22-8e65-a9519cc02544 /如何到下载一个文件,从SharePoint的-2013-利用休息和-C?论坛= sharepointdevelopment


或者我们也可以使用 WebClient 和REST API从文档库中下载文件。


https://social.msdn.microsoft.com/Forums/en-US/f7f1cb7d-4406-4b72-b7f1-dc5dde6c34e1/how-to-download-file-from-document-librar y-using-rest-api?forum = appsforsharepoint


最好的问候,


Dennis



I am using the following endpoint

url: http://site url/_api/web/GetFolderByServerRelativeUrl('/Folder Name')/Files('file name')/$value
method: GET

It is working fine if I browse the endpoint in the browser. When I tried with httpwebrequest and getresponsestream() function it is not returning the file stream.

Please do the needful

解决方案

Hi,

The following code snippet for your reference.

var siteURL = "http://sp2013/sites/team";
var username = "dennis";
var password = "password";
var domain = "contoso";

var folderRelativeUrl = "/sites/team/DL";
var fileName = "Test.pdf";

HttpWebRequest request = null;
HttpWebResponse response = null;
string commandString = string.Empty;

commandString = string.Format("/_api/web/GetFolderByServerRelativeUrl('{0}')/files('{1}')/


value",folderRelativeUrl,fileName); Uri uri = new Uri(siteURL + commandString); //Set up the HTTP Request request = (HttpWebRequest)WebRequest.Create(uri); request.Credentials = new NetworkCredential(username, password, domain); request.Method = WebRequestMethods.Http.Get; response = (HttpWebResponse)request.GetResponse(); if (response.ContentLength > 0) response.ContentLength = response.ContentLength; Stream output = response.GetResponseStream(); using (var fs = new FileStream("c:\\temp\\"+fileName, FileMode.OpenOrCreate)) output.CopyTo(fs);

More information:

https://social.technet.microsoft.com/Forums/sharepoint/en-US/f6b1f3c2-d8e5-4d22-8e65-a9519cc02544/how-to-download-a-file-from-sharepoint-2013-using-rest-and-c?forum=sharepointdevelopment

Or we can also use WebClient with REST API to download file from document library.

https://social.msdn.microsoft.com/Forums/en-US/f7f1cb7d-4406-4b72-b7f1-dc5dde6c34e1/how-to-download-file-from-document-library-using-rest-api?forum=appsforsharepoint

Best Regards,

Dennis


这篇关于使用sharepoint rest api c#httpwebrequest下载文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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