Click here to Skip to main content
15,881,757 members
Articles / Web Development / ASP.NET

Silverlight Pronunciation Test

Rate me:
Please Sign up or sign in to vote.
5.00/5 (12 votes)
29 Apr 2012CPOL11 min read 43.7K   1.7K   15  
How to create a pronunciation test tool using Silverlight and Python
// Copyright (c) 2010
// by OpenLight Group
// http://openlightgroup.net/
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
//
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.


using System;
using System.Linq;
using System.IO;
using System.Web;
using System.Collections.Generic;

namespace PitchContour
{
    public delegate void FileUploadCompletedEvent(object sender, FileUploadCompletedEventArgs args);

    #region FileUploadCompletedEventArgs
    public class FileUploadCompletedEventArgs
    {
        public string FileName { get; set; }
        public string FilePath { get; set; }

        public FileUploadCompletedEventArgs() { }

        public FileUploadCompletedEventArgs(string fileName, string filePath)
        {
            FileName = fileName;
            FilePath = filePath;
        }
    }
    #endregion

    #region class FileUploadProcess
    public class FileUploadProcess
    {
        public event FileUploadCompletedEvent FileUploadCompleted;

        #region ProcessRequest
        public void ProcessRequest(HttpContext context)
        {
            // Values passed

            string Originalfilename = string.IsNullOrEmpty(context.Request.QueryString["filename"]) ? "Unknown" : context.Request.QueryString["filename"];
            bool complete = string.IsNullOrEmpty(context.Request.QueryString["Complete"]) ? true : bool.Parse(context.Request.QueryString["Complete"]);
            bool getBytes = string.IsNullOrEmpty(context.Request.QueryString["GetBytes"]) ? false : bool.Parse(context.Request.QueryString["GetBytes"]);
            long startByte = string.IsNullOrEmpty(context.Request.QueryString["StartByte"]) ? 0 : long.Parse(context.Request.QueryString["StartByte"]); ;

            string strExtension = System.IO.Path.GetExtension(Originalfilename);
            string strFileDirectory = context.ApplicationInstance.Server.MapPath(@"~\Files\");
            strFileDirectory = strFileDirectory + @"\";
            string filePath = Path.Combine(strFileDirectory, Originalfilename);

            // Check File Extension
            // This will throw a fatal exception, but the Client should 
            // catch the invalid extension before it gets this far
            if (!CheckFileExtension(strExtension))
            {
                context.Response.Write("0");
                context.Response.Flush();
                return;
            }

            if (getBytes)
            {
                FileInfo fi = new FileInfo(filePath);

                // If file exists - delete it
                if (fi.Exists)
                {
                    try
                    {
                        fi.Delete();
                    }
                    catch
                    {
                        // could not delete
                    }
                }

                context.Response.Write("0");
                context.Response.Flush();
                return;
            }
            else
            {
                if (startByte > 0 && File.Exists(filePath))
                {
                    using (FileStream fs = File.Open(filePath, FileMode.Append))
                    {
                        SaveFile(context.Request.InputStream, fs);
                        fs.Close();
                    }
                }
                else
                {
                    using (FileStream fs = File.Create(filePath))
                    {
                        SaveFile(context.Request.InputStream, fs);
                        fs.Close();
                    }
                }
                if (complete)
                {
                    if (FileUploadCompleted != null)
                    {
                        FileUploadCompletedEventArgs args = new FileUploadCompletedEventArgs(Originalfilename, filePath);
                        FileUploadCompleted(this, args);
                    }
                }
            }
        }
        #endregion

        #region SaveFile
        private void SaveFile(Stream stream, FileStream fs)
        {
            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) != 0)
            {
                fs.Write(buffer, 0, bytesRead);
            }
        }
        #endregion

        #region CheckFileExtension
        private bool CheckFileExtension(string strExtension)
        {
            if (
             string.Compare(Path.GetExtension(strExtension).ToLower(), ".gif", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".jpg", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".jpeg", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".doc", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".docx", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".xls", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".xlsx", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".pdf", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".txt", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".png", true) != 0
             & string.Compare(Path.GetExtension(strExtension).ToLower(), ".wav", true) != 0
             )
            {
                return false;
            }
            else
            {
                return true;
            }
        } 
        #endregion
    }
    #endregion
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Instructor / Trainer Alura Cursos Online
Brazil Brazil

Comments and Discussions