Csharp/C#教程:将web api blob读入一个字符串,我可以将其作为json对象的一部分发送到服务器并转回文件分享


将web api blob读入一个字符串,我可以将其作为json对象的一部分发送到服务器并转回文件

我正在尝试将wav文件转换为字符串,我可以将其作为json对象的一部分发送到服务器,以便在服务器上我可以将该字符串转换回文件。

我曾尝试使用readAsBinaryString并将其作为文本读取,在将字符串读入字节数组时无法获得过去的错误。

reader.onloadend = saveMedia; reader.readAsText(Blob); //reader.readAsBinaryString(Blob); also tried. 

然后回调发送一个ajax请求,其中一个对象持有“reader.result”中的字符串,并在服务器上我尝试过如下操作:

 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] BinaryData = encoding.GetBytes(stringFromRequest); 

下面这个问题的答案似乎是不应该这样做。 但我真的想这样做,因为我使用的另一个工具(微风js)。 不希望对文件数据类型使用单独的post操作。

相关: 文件API – Blob到JSON

找到一种有效的方法:

  var reader = new FileReader(); reader.onloadend = afterRead; reader.readAsBinaryString(blob); function afterRead() { // convert binary string to base64 for a safe transfer to the server. entity.BinaryProp = window.btoa(reader.result); } 

在服务器端:

 string BinaryString = (string)entityInfo.UnmappedValuesMap["BinaryProp"]; byte[] BinaryData = Convert.FromBase64String(BinaryString); 

上面的答案很棒,但有一种更简单的方法。

 var reader = new FileReader(); reader.onloadend = afterRead; reader.readAsDataURL(blob); // Use this function instead function afterRead() { entity.BinaryProp = reader.result; //result is already a base64 string! } 

请参阅此处的文档: FileReader.readAsDataURL()

上述就是C#学习教程:将web api blob读入一个字符串,我可以将其作为json对象的一部分发送到服务器并转回文件分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/1009958.html

(0)
上一篇 2021年12月29日
下一篇 2021年12月29日

精彩推荐