我找到了这段代码
// "url" is the full destination path (including filename, i.e. https://mysite.sharepoint.com/Documents/Test.txt)
// "cookie" is the CookieContainer generated from Wichtor's code
// "data" is the byte array containing the files contents (used a FileStream to load)
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "PUT";
request.Accept = "*/*";
request.ContentType = "multipart/form-data; charset=utf-8";
request.CookieContainer = cookie; request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
request.Headers.Add("Accept-Language", "en-us");
request.Headers.Add("Translate", "F"); request.Headers.Add("Cache-Control", "no-cache"); request.ContentLength = data.Length;
using (Stream req = request.GetRequestStream())
{ req.Write(data, 0, data.Length); }
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream res = response.GetResponseStream();
StreamReader rdr = new StreamReader(res);
string rawResponse = rdr.ReadToEnd();
response.Close();
rdr.Close();
显然已成功将大型文件上传到 sharepoint 网站,但我的代码位于 powershell 。无论如何,现在在powershell中使用它的形式,甚至将其转换为PowerShell?
答案 0 :(得分:15)
需要注意的一些事项:
C#是一种类型安全的语言,这意味着编译器保证变量的类型及其值,如示例所示:
HttpWebRequest request = HttpWebRequest.Create(url);
// ^ ^ \________________________/^
// | | | |
// | variable name | statement terminator ";"
// type-name |
// static method call that returns a
// value we can assign to "request"
在PowerShell中:
$
Create()
方法),我们使用以下语法:
[Namespace.TypeName]::Member
;
是不必要的,在语句暗示终止后的换行符因此,上述陈述变为:
$request = [System.Net.HttpWebRequest]::Create($url)
两个C#布尔关键字(true
和false
)在PowerShell中由两个名为$true
和$false
的自动变量表示:
[System.Net.ServicePointManager]::Expect100Continue = $false
using
PowerShell没有与C#'s using
statement相当的构造。为了确保处理实现IDisposable
的对象,您必须使用try
/ catch
/ finally
:
$req = $request.GetRequestStream()
try{
$req.Write($data, 0, $data.Length)
} catch {
throw $_
} finally {
if($req){
$req.Dispose()
}
}
PowerShell没有用于对象实例化的new
关键字,但提供了可以包装C#构造函数的New-Object
cmdlet:
$rdr = New-Object -TypeName System.IO.StreamReader -ArgumentList $res
而不是:
StreamReader rdr = new StreamReader(res);
在PowerShell 5.0及更高版本中,您现在可以使用new
静态方法调用构造函数:
$rdr = [System.IO.StreamReader]::new($res)
PowerShell支持显式转换,它在C#中看起来像(typename)variable
,但同样,使用方括号而不是括号:
[System.Net.HttpWebResponse]$request.GetResponse()
并且(从版本3.0开始)它也支持未选中的转换(仍然带有方括号):
$request.GetResponse() -as [System.Net.HttpWebResponse]
如果无法进行演员,后者将返回$null
,而不是抛出错误。
这应该让你立刻翻译。根据您的codefind中的注释,您似乎也需要翻译“Wichtor的代码”,以便生成$cookie
容器。
答案 1 :(得分:1)
见下面的例子。我必须在类和函数中包装代码。
$code = @"
using System.Security.Policy;
using System.IO;
using System.Net;
public class MySharepointTools
{
public void UploadFile(System.String url, System.Net.CookieContainer cookie, System.Byte[] data)
{
System.Net.ServicePointManager.Expect100Continue = false;
HttpWebRequest request = HttpWebRequest.Create(url) as HttpWebRequest;
request.Method = "PUT";
request.Accept = "*/*";
request.ContentType = "multipart/form-data; charset=utf-8";
request.CookieContainer = cookie; request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0)";
request.Headers.Add("Accept-Language", "en-us");
request.Headers.Add("Translate", "F"); request.Headers.Add("Cache-Control", "no-cache"); request.ContentLength = data.Length;
using (Stream req = request.GetRequestStream())
{ req.Write(data, 0, data.Length); }
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream res = response.GetResponseStream();
StreamReader rdr = new StreamReader(res);
string rawResponse = rdr.ReadToEnd();
response.Close();
rdr.Close();
}
}
"@
Add-Type -TypeDefinition $code -Language CSharp
$tools = New-Object MySharepointTools
现在您可以使用
调用该方法$Tools.UploadFile(url, cookie, data)