录音机,将Blob文件保存到服务器 - C#,Mvc

Ram*_*gün 6 c# audio asp.net-mvc jquery blob

我需要在我正在进行的项目中使用录音机,并且必须在以后收听录制的声音.该项目由c#和asp.net mvc开发.

http://demos.subinsb.com/jquery/voice/

我正在上面的链接中使用录像机系统.单击"下载"时,将为您提供文件.wav格式.它被下载到您的计算机,但我想将其保存到服务器.

该项目的源代码可在链接下方找到.

http://demos.subinsb.com/down.php?id=s/rvx90xq1xtpak3xc647n&class=31

我调查jquery代码:当您单击下载时

 $(document).on("click", "#download:not(.disabled)", function () {
            $.voice.export(function (url) {
                $("<a href='" + url + "'download='MyRecording.wav'></a>")[0].click();
            }, "URL");
            restore(); });
Run Code Online (Sandbox Code Playgroud)

它正在使用此功能.函数中的url参数响应链接为

blob:http%3A//localhost%3A1875/146432b2-8b1b-4eef-8d77-1fdbc8fc74c9
Run Code Online (Sandbox Code Playgroud)

当你在播放器wav文件中使用adres工作但是播放器stil中的src有以下代码

blob:http%3A//localhost%3A1875/146432b2-8b1b-4eef-8d77-1fdbc8fc74c9
Run Code Online (Sandbox Code Playgroud)

题:

如何将声音文件保存到服务器.我无法从本网站的问题和一般在互联网上找到任何解决方案:)

1 - 我必须使用Jquery Ajax将哪个参数发送给控制器?

2 - 如何在控制器端将该参数转换为wav或mp3格式?

从现在开始,非常感谢你们:)

小智 5

我使用recorder.js是因为我想要自定义-我只需要可以在自己的UI中使用的回调。

首先,下面是相关的JavaScript代码:

Recorder.js上传功能

doUpload: function(title, filename) {
    Recorder.upload({
        method: "POST",
        url: "@Url.Action("Upload", "Recordings")",
        audioParam: "Recording", // Name for the audio data parameter
        params: {
            // Additional parameters need to be an object
            "Title": title,
            "FileName": filename
        },
        success: function(response) {
            console.log(response);
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

在服务器端,这非常简单。您可以使用带有HttpPostedFileBase参数的常规控制器操作来处理文件输入。另一种方法是使用Request.Files。但是,就我而言,我使用数据模型来接收输入。

VoicePassage类

public class VoicePassage
{
    public string Title { get; set; }
    public string FileName { get; set; }
    public HttpPostedFileBase Recording { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

有关如何保存文件的精简版本。这真的很愚蠢。您应该使用数据模型上的标准或自定义ValidateAttribute来验证输入。数据模型中的某处也应该有一个自定义的[MimeType(“ audio / wav”)]属性。

精简版如何保存文件

public JsonResult Upload(VoicePassage passage)
{
    // Validate the input
    // ...
    // ...

    // Save the file
    string path = Server.MapPath(string.Format("~/Recordings/{0}.wav", passage.FileName));
    passage.Recording.SaveAs(path);

    return Json(new { Success: true });
}
Run Code Online (Sandbox Code Playgroud)

Recorder.upload()函数向服务器发出AJAX请求,因此有意义的是返回JsonResult而不是ActionResult。回到客户端,您可以简单地处理结果并采取措施,例如将其追加到列表或显示错误。