0

私はこのコードを持っており、正常に動作しますが、ユーザーがダウンロードして聞くのではなく、ページに埋め込むことができる wav ファイルを出力する方法に苦労しています。どんな助けでも大歓迎です。キャプチャ テキストと一致するように、クエリ文字列を使用してファイルを直接呼び出すことができるようにしたいと考えています。Model.InstanceData["Code"] はテキスト文字列のキャプチャです。私はそれについて頭を包むことができないようです。ありがとう!

private void _playBtn_Click(object sender, ImageClickEventArgs e)
{
    HttpContext context = HttpContext.Current;

    if (context == null || context.Response == null)
    {
        return;
    }

    context.Response.Clear();

    context.Response.AddHeader("content-disposition", "attachment; filename=" + "captcha.wav");
    context.Response.AddHeader("content-transfer-encoding", "binary");

    context.Response.ContentType = "audio/wav";

    SoundGenerator soundGenerator = new SoundGenerator(Model.InstanceData["Code"]);

    MemoryStream sound = new MemoryStream();

    // Write the sound to the response stream 
    soundGenerator.Sound.Save(sound, SoundFormatEnum.Wav);

    sound.WriteTo(context.Response.OutputStream);
}
4

1 に答える 1

0

基本的に、ソリューションをいろいろと試してみた結果、思ったよりも少し簡単でした。プロジェクトで新しい CaptchaPlayer.aspx プレーヤーを作成し、コード ビハインドでこれを使用しました。

 protected void Page_Load(object sender, EventArgs e)
    {
        PlayCaptchaSession();
    }

    private void PlayCaptchaSession()
    {
        object o = Session["captcha"];

        if (o != null)
        {
            HttpContext context = HttpContext.Current;

            if (context == null || context.Response == null)
            {
                return;
            }

            //context.Response.AddHeader("content-disposition", "inline; filename=" + "captcha.wav");
            context.Response.AddHeader("content-transfer-encoding", "binary");
            context.Response.ContentType = "audio/wav";
            SoundGenerator soundGenerator = new SoundGenerator(o as string);
            MemoryStream sound = new MemoryStream();
            // Write the sound to the response stream 
            soundGenerator.Sound.Save(sound, SoundFormatEnum.Wav);
            sound.WriteTo(context.Response.OutputStream);
        }
    }

}

その後、フォームページに埋め込みを追加できました

 <embed id='captchaAudio' width='1' height='1' enablejavascript='true' autostart='false' name='Verification code player' src='/CaptchaPlayer.aspx'>
                <noembed><a href='/CaptchaPlayer.aspx' title='Verification code wave audio file download'>Audio File</a></noembed>
                 <script type="text/javascript">
                 $(document).ready(function(){
                     $(".capLink").click(function () {
                         //alert(".click() called.");
                         document.getElementById('captchaAudio').Play();

                     });
                 });    
                </script>

そして、それはトリックをしました!これが誰かに役立つことを願っています。

于 2013-04-23T20:36:04.323 に答える