Sending blob files to server

前端 未结 1 1325
孤独总比滥情好
孤独总比滥情好 2021-01-14 13:41

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 t

1条回答
  •  再見小時候
    2021-01-14 14:07

    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

    public uploadFile(file: File): Observable {
       const formData = new FormData();
       formData.Append('myFile', file);
       return this.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    
    public async Task 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();
    }
    

    0 讨论(0)
提交回复
热议问题