将Blob文件发送到服务器 [英] Sending blob files to server

查看:672
本文介绍了将Blob文件发送到服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将录音发送到服务器并将其另存为.wav.我在前端使用尖角,在服务器上使用.net核心.我能够进行录制,然后创建一个类型为"audio/wav"的blob.为了将其发送到服务器,我将其转换为数组缓冲区,然后将数组缓冲区转换为base64字符串,并将其发布到控制器.

I am trying to send a audio recording to server and save it as .wav. I am using angular on the front end and .net core on server. I was able to record, then make a blob of type "audio/wav". For sending it to server, I convert it into an array buffer and then the array buffer to base64 string which I post to controller.

在服务器端,当我将这些字节(从base 64中提取数组缓冲区后)写入wav文件时,无法播放它.有人可以帮助我在.net控制器方面做错什么.

On the server side when I write those bytes(after extracting array buffer from base 64), to a wav file, I cant play it. Can someone help me what I am doing wrong on the .net controller side.

如果有人知道更干净的方法,请告诉我

If someone knows a cleaner way of doing this, please let me know

推荐答案

您不必创建数组缓冲区.只需使用文件输入并发送表单数据即可.

You don't have to create an array buffer. Just use a file-input and send form-data.

假设您使用的是angular 4.3 ++,并使用@angular/common/http中的 HttpClientModule :

Assuming you are on angular 4.3++ and using HttpClientModule from @angular/common/http:

角度服务方法

public uploadFile(file: File): Observable<any> {
   const formData = new FormData();
   formData.Append('myFile', file);
   return this.http.post('my-api-url', formData);
}

现在您是asp.net核心终结点

now you asp.net-core endpoint

[HttpPost]
 // attention name of formfile must be equal to the key u have used for formdata    
public async Task<IActionResult> UploadFileAsync([FromForm] IFormFile myFile)
{
     var totalSize = myFile.Length;
     var fileBytes = new byte[myFile.Length];

     using (var fileStream = myFile.OpenReadStream())
     {
         var offset = 0;

         while (offset < myFile.Length)
         {
             var chunkSize = totalSize - offset < 8192 ? (int) totalSize - offset : 8192;

             offset += await fileStream.ReadAsync(fileBytes, offset, chunkSize);
         }
     }
   // now save the file on the filesystem
   StoreFileBytes("mypath", fileBytes);
   return Ok();
}

这篇关于将Blob文件发送到服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆