C# WebAPI2.0上传视频
时间: 2024-02-05 10:07:59 浏览: 155
视频上传demo (v2.0)(最新修改版)
以下是C# WebAPI2.0上传视频的示例代码:
1. WebAPI端代码
```csharp
[HttpPost]
public async Task<IActionResult> UploadVideo()
{
var file = Request.Form.Files[0];
var filePath = "your file path"; // 设置文件保存路径
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
return Ok();
}
```
2. C#客户端代码
```csharp
public async Task UploadVideo(string filePath)
{
using (var client = new HttpClient())
{
using (var content = new MultipartFormDataContent())
{
using (var fileStream = new FileStream(filePath, FileMode.Open))
{
content.Add(new StreamContent(fileStream), "file", Path.GetFileName(filePath));
var response = await client.PostAsync("your WebAPI url", content);
if (!response.IsSuccessStatusCode)
{
throw new Exception("上传失败");
}
}
}
}
}
```
注意:在WebAPI端代码中,需要设置文件保存路径;在C#客户端代码中,需要将文件流添加到MultipartFormDataContent中,并指定文件名和WebAPI的URL。
阅读全文