Skip to content Skip to sidebar Skip to footer

Sending Blob Files To Server

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 'au

Solution 1:

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

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

The angular-service method

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

now you asp.net-core endpoint

[HttpPost]
 // attention name of formfile must be equal to the key u have used for formdata    publicasync Task<IActionResult> UploadFileAsync([FromForm] IFormFile myFile)
{
     var totalSize = myFile.Length;
     var fileBytes = newbyte[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();
}

Post a Comment for "Sending Blob Files To Server"