使用 --upload-file 上传文件到服务器

upload file with --upload-file to server

我目前正在尝试创建一个休息端点以使用 curl 上传文件。

为此,我想使用名为 --upload-file 的超棒内置参数,它将文件上传到服务器。

This has the following properties:

例如curl --upload-file "texts/test.txt" http://localhost/

它向 http://localhost/file.txt

发送了一个 PUT 请求

我现在如何在控制器中定义一个动作来捕获所有放置请求?

我试过:

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 

感谢 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);
    }
}