提问人:Romain 提问时间:10/22/2023 最后编辑:Brian Tompsett - 汤莱恩Romain 更新时间:11/17/2023 访问量:43
将视频文件从 blazor webassembly 上传到 api.videoUpload a video file to api.video, from blazor webassembly
Upload a video file to api.video, from blazor webassembly
问:
我在尝试将视频上传到 api.video 时遇到困难
我在 Blazor 上,.Net 核心 8。
这是他们的文档:https://docs.api.video/reference/api/Videos,特别是“上传视频”部分。
我可以做其他所有事情,AccessToken,删除视频,创建视频等......
这是我所拥有的:
public async Task OnSelectFile (InputFileChangeEventArgs e)
{
if (e.File != null)
{
long maxFileSize = 1024L * 1024L * 1024L * 2L;
string contentType = "";
string sourceB64 = "";
IBrowserFile videoFile = e.File;
string file;
var buffers = new byte[videoFile.Size];
await videoFile.OpenReadStream(maxFileSize).ReadAsync(buffers);
file = Convert.ToBase64String(buffers);
if (await videoClient.UploadVideo(currentVideoObject, file))
{
toast.ShowToast("Successfully uploaded Video", ToastLevel.Success);
}
else
{
toast.ShowToast("Failed to upload video", ToastLevel.Error);
}
}
}
public async Task<bool> UploadVideo (VideoObject video, string file)
{
try
{
var request = new HttpRequestMessage(new HttpMethod("Post"), "https://ws.api.video/videos/"+video.Id+"/source");
request.Headers.TryAddWithoutValidation("accept", "application/json");
request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", token.accesstoken);
request.Content = new StringContent("{\"bytes\":\""+file+"\"}");
var response = await client.SendAsync(request);
if (response.IsSuccessStatusCode)
{
return true;
}
}
catch (Exception ex)
{
Console.WriteLine("Failed to upload video" + ex);
}
return false ;
}
我得到的回应是:
错误请求,则请求中不存在该文件。
答:
0赞
Artem
11/17/2023
#1
您不能将文件作为字符串传递,因为 api.video API 只接受多部分形式。您必须确保通过从磁盘读取文件来传入文件流。您可以通过将代码更改为如下所示来做到这一点:
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://ws.api.video/videos/:videoId/source");
request.Headers.Add("Content-Range", "");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("Authorization", "Basic base_64_api_key");
var content = new MultipartFormDataContent();
content.Add(new StreamContent(File.OpenRead("/path/to/file.mp4")), "file", "/path/to/file.mp4");
request.Content = content;
var response = await client.SendAsync(request);
response.EnsureSuccessStatusCode();
Console.WriteLine(await response.Content.ReadAsStringAsync());
评论