我已成功预览图像,然后通过http请求API 使用以下代码进行上传:
// For Image Preview
async Task LoadImage(InputFileChangeEventArgs e) {
var format = "image/jpg";
var imageFile = await e.File.RequestImageFileAsync(format, 640, 480);
fileStream = imageFile.OpenReadStream(maxFileSize);
_fileName = e.File.Name;
MediaStream = new MemoryStream();
await fileStream.CopyToAsync(MediaStream);
imageDataUri = $"data:format};base64,Convert.ToBase64String(MediaStream.ToArray())}";
}
// For API request, using multipart form data as I need to send a json object along with the image
protected async Task HandleValidSubmit() {
_disabledSubmitButton = true;
//create content headers
var content = new MultipartFormDataContent();
content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("form-data");
string json = JsonConvert.SerializeObject(PostForCreation);
content.Add(new StringContent(json), "fromRequestPost");
MediaStream.Position = 0;
content.Add(new StreamContent(MediaStream, (int)MediaStream.Length), "fromRequestMediaFile", _fileName);
////call to the server
await PostService.CreatePost(content);
_disabledSubmitButton = false;
}
我的问题是,如何为视频文件实现相同的功能?我希望能够在选择视频后将视频加载到内存中(例如下面的“ LoadImage”功能),然后在提交后将其传递给多部分API请求(例如下面的“ HandleValidSubmit”函数)。 API服务的实现就像普通的POST,因此不相关,因此不包含在问题中。
感谢您的帮助!