我试图了解Powershell如何将数据发送到服务器,并创建了一个ASP.NET MVC项目和一个单独的Powershell脚本。
MVC操作看起来像:
[HttpPost]
public ActionResult Upload(HttpPostedFileBase file, string name, string description, string descr)
{
return new EmptyResult();
}
以及Powershell脚本,该脚本应上载文件以及一些信息:
Add-Type -AssemblyName "System.Web"
$mainUrl= "http://localhost:27701";
function upload {
param(
[string]$filePath,
[string]$description,
[string]$genre
)
if(![System.IO.File]::Exists($filePath)) {
Write-Error "$filePath does not exist";
return;
}
$url = $mainUrl + "/Home/Upload";
$name = [System.IO.Path]::GetFileNameWithoutExtension($filePath);
$filename = [System.IO.Path]::GetFileName($filePath);
$dt = [DateTime]::Now.Ticks.ToString("x", [System.Globalization.NumberFormatInfo]::InvariantInfo);
$boundary = "---------------------------$dt";
$fileBytes = [System.IO.File]::ReadAllBytes($filePath);
$fileEnc = [System.Text.Encoding]::GetEncoding('UTF-8').GetString($fileBytes);
$fileContentType = [System.Web.MimeMapping]::GetMimeMapping($filename);
$newLine = "`r`n";
$bodyLines = (
"--$boundary",
"Content-Disposition: form-data; name=`"file`"; filename=`"$filename`"",
"Content-Type: $fileContentType$newLine",
$fileEnc,
"--$boundary",
"Content-Disposition: form-data; name=`"name`"$newLine",
$name,
"--$boundary",
"Content-Disposition: form-data; name=`"description`"$newLine",
$genre,
"--$boundary",
"Content-Disposition: form-data; name=`"descr`"$newLine",
$description,
"--$boundary"
) -join $newLine;
$contentType = "multipart/form-data";
$params1 = @{
"name" = $name;
"description" = $description;
"descr" = $genre;
"file" = Get-Item $filePath;
}
Invoke-WebRequest -Uri $url -Method Post -Body $params1 -ContentType $contentType;
}
upload -filePath "C:\test.txt" -description "Test" -genre "genre";
我试了两种方法:
-Body $params1
和
-Body $bodyLines
,我仍然收到
file
,
name
,
description
和
descr
零。
我还是不明白为什么我会收到零分。我的错误在哪里?