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

时间:2015-01-20 08:37:35

标签: c# jquery asp.net-mvc audio 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(); });

它正在使用此功能。函数中的url参数将您的链接作为

回复
blob:http%3A//localhost%3A1875/146432b2-8b1b-4eef-8d77-1fdbc8fc74c9

当你在播放器中播放时,wav文件正在运行,但播放器stil中的src有以下代码

blob:http%3A//localhost%3A1875/146432b2-8b1b-4eef-8d77-1fdbc8fc74c9

问题:

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

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

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

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

1 个答案:

答案 0 :(得分:4)

我选择了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);
        }
    });
}

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

VoicePassage类

public class VoicePassage
{
    public string Title { get; set; }
    public string FileName { get; set; }
    public HttpPostedFileBase Recording { get; set; }
}

如何保存文件的愚蠢版本。这个真的很愚蠢。您应该在数据模型上使用标准或自定义ValidateAttribute / s验证输入。在某个地方的数据模型中应该有一个自定义[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 });
}

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