我目前正在尝试创建一个休息端点来上传带有curl的文件。
为此,我想使用名为--upload-file
的出色内置参数,该参数将文件上传到服务器。
This has the following properties:
例如curl --upload-file "texts/test.txt" http://localhost/
它向PUT
发送http://localhost/file.txt
请求
现在如何在控制器内定义一个动作来捕获所有放置请求?
我尝试过:
public class HomeController : Controller {
[HttpPut]
[ActionName("Index")]
public string UploadFilePut() {
}
}
和
public class HomeController : Controller {
[HttpPut]
[ActionName("Index")]
public string UploadFilePut(IFormFile file) {
}
}
但它们都不起作用。
编辑:
curl localhost:57623 --upload-file diamond.sh -v
% Total % Received % Xferd Average Speed Time Time Time
Current
Dload Upload Total Spent Left Speed
0 0 0 0 0 0 0 0 --:--:-- --:--:-- --:--:-- 0* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 57623 (#0)
> PUT /diamond.sh HTTP/1.1
> Host: localhost:57623
> User-Agent: curl/7.60.0
> Accept: */*
> Content-Length: 1987
> Expect: 100-continue
>
< HTTP/1.1 100 Continue
} [1987 bytes data]
* We are completely uploaded and fine
< HTTP/1.1 404 Not Found
< Date: Tue, 17 Jul 2018 17:47:09 GMT
< Server: Kestrel
< Content-Length: 0
<
100 1987 0 0 100 1987 0 64096 --:--:-- --:--:-- --:--:-- 64096
* Connection #0 to host localhost left intact
在服务器上:
Request starting HTTP/1.1 PUT http://localhost:57623/diamond.sh 1987
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[2]
Request finished in 13.7902ms 404
答案 0 :(得分:0)
感谢Mark G,这是正确执行操作的方法:
[HttpPut("{filename}.{ext?}")]
[ActionName("Index")]
public void UploadPut(string filename, string ext) {
var tmp = new FileInfo("temp");
Console.WriteLine($"Writing {filename}.{ext} to {tmp.FullName}");
using (var fs = tmp.OpenWrite()) {
Request.Body.CopyTo(fs);
}
}