Play MP4 video containing special characters in file name using C# and VB.Net in ASP.Net

Last Reply on Apr 19, 2017 10:04 AM By Indresh

Posted on Apr 17, 2017 02:18 AM

I maked this post as answered before when it wasnt uploading to folder with such charachters above, but now it cant play any mp4 with these charachers # - , I decided to remove those characters from the video name and it start playing, its very difficult to alow such error since i tell if users will end up uploading videos with such charachter. Bastille Day Official International Trailer #1 (2016) - Idris Elba, Richard Madden Action Movie HD The on below is without those characters and its playing but the one above is not Bastille Day Official International Trailer1 (2016) Idris Elba, Richard Madden Action Movie HD Link for the previous solved problem

http://www.aspforums.net/Threads/116088/Filupload-not-uploading-video-names-with-these-characters--/Replies/10#Replies

protected void LinkBPOST_Click(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/path/"); //Please enter your folder Name here.
    if (this.Page.User.Identity.IsAuthenticated)
    {
        string username = this.Page.User.Identity.Name;
        {
            string tendString = TextBixcomment.Text.Trim();
            string strname = "";
            string extension = "";
            string getADPOST = "";
            string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            if (FileUploadpost.HasFile)
            {
                strname = FileUploadpost.FileName.ToString();
                extension = System.IO.Path.GetExtension(strname);
                using (SqlConnection con = new SqlConnection(str))
                {
                    SqlCommand cmd = new SqlCommand();
                    // Image extension.
                    if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png" || extension.ToLower() == ".gif" || extension.ToLower() == ".jpeg")
                    {
                        getADPOST = "Insert INTO USERPost (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
                        cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
                        cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                        cmd.Parameters.AddWithValue("@ImageName1", strname);
                    }
                    // Video extension.
                    else if (extension.ToLower() == ".mp4" || extension.ToLower() == ".wmv" || extension.ToLower() == ".flv" || extension.ToLower() == ".vob" || extension.ToLower() == ".dat")
                    {
                        getADPOST = "Insert INTO USERPost (UserName,ContentPost,Path) values (@UserName,@ContentPost,@Path)";
                        cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
                        cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                        cmd.Parameters.AddWithValue("@Path", "/path/" + strname);
                    }
                    cmd.CommandText = getADPOST;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    FileUploadpost.SaveAs(folderPath + Path.GetFileName(FileUploadpost.FileName)); //save file from fileUpload to folder here.
                    string message = "Post submitted successfully.";
                    //   string script = "window.onload = function(){ alert('";
                    //script += message;
                    // script += "')};";
                    // Page.ClientScript.RegisterStartupScript(this.GetType(), "SuccessMessage", script, true);
                    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal5('" + message + "');", true);
                }
            }
        }
 
        this.Populatebooks();
    }
}
Posted on Apr 19, 2017 10:04 AM

Hi micah,

I have created sample code which full-fill your requirement. So please refer the below code and modify as per your requirement.

HTML

<div>
    <asp:FileUpload ID="FileUploadpost" runat="server" />
    <br />
    <br />
    <asp:TextBox ID="TextBixcomment" Width="250px" Height="50px" Placeholder="Enter your message"
        runat="server" />
    <asp:Button ID="Button1" Text="Upload" OnClick="LinkBPOST_Click" runat="server" />
    <br />
    <asp:DataList ID="dlPost" runat="server">
        <ItemTemplate>
            <video id="video" runat="server" controls="controls" width="300px" class="w3-margin-bottom">
                <source src='<%#Eval("FileName")%>'  runat="server" type="video/mp4" />
                Tarayıcınız video etiketini desteklemiyor!
                </video>
        </ItemTemplate>
    </asp:DataList>
</div>

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        this.DisplayVideo();
    }
}

protected void LinkBPOST_Click(object sender, EventArgs e)
{
    string folderPath = Server.MapPath("~/path/"); //Please enter your folder Name here.
    if (this.Page.User.Identity.IsAuthenticated)
    {
        string username = this.Page.User.Identity.Name;
        {
            string tendString = TextBixcomment.Text.Trim();
            string strname = "";
            string extension = "";
            string getADPOST = "";
            string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
            if (FileUploadpost.HasFile)
            {
                strname = FileUploadpost.FileName.ToString();
                extension = System.IO.Path.GetExtension(strname);
                using (SqlConnection con = new SqlConnection(str))
                {
                    SqlCommand cmd = new SqlCommand();
                    // Image extension.
                    if (extension.ToLower() == ".jpg" || extension.ToLower() == ".png" || extension.ToLower() == ".gif" || extension.ToLower() == ".jpeg")
                    {
                        getADPOST = "Insert INTO File (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
                        cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
                        cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                        cmd.Parameters.AddWithValue("@ImageName1", strname);
                    }
                    // Video extension.
                    else if (extension.ToLower() == ".mp4" || extension.ToLower() == ".wmv" || extension.ToLower() == ".flv" || extension.ToLower() == ".vob" || extension.ToLower() == ".dat")
                    {
                        getADPOST = "Insert INTO File (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)";
                        cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim());
                        cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
                        cmd.Parameters.AddWithValue("@ImageName1", "path/" + strname);
                    }
                    cmd.CommandText = getADPOST;
                    cmd.CommandType = System.Data.CommandType.Text;
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                    FileUploadpost.SaveAs(folderPath + Path.GetFileName(FileUploadpost.FileName)); //save file from fileUpload to folder here.
                    string message = "Post submitted successfully.";
                    ClientScript.RegisterStartupScript(this.GetType(), "Pop", "openModal5('" + message + "');", true);
                }
            }
        }

        this.DisplayVideo();
    }
}

private void DisplayVideo()
{
    string str = ConfigurationManager.ConnectionStrings["DB"].ConnectionString;
    SqlConnection con = new SqlConnection(str);
    SqlCommand cmd = new SqlCommand("SELECT * FROM [File]", con);
    SqlDataAdapter sda = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    sda.Fill(dt);
    DataTable dt1 = dt.Clone();
    foreach (DataRow dr in dt.Rows)
    {
        dt1.Rows.Add(dt.Rows[0]["Id"], Server.UrlEncode(dt.Rows[0]["FileName"].ToString()));
    }
    dlPost.DataSource = dt1;
    dlPost.DataBind();
}

Vb.net

Protected Sub Page_Load(sender As Object, e As EventArgs)
	If Not IsPostBack Then
		Me.DisplayVideo()
	End If
End Sub

Protected Sub LinkBPOST_Click(sender As Object, e As EventArgs)
	Dim folderPath As String = Server.MapPath("~/path/")
	'Please enter your folder Name here.
	If Me.Page.User.Identity.IsAuthenticated Then
		Dim username As String = Me.Page.User.Identity.Name
		If True Then
			Dim tendString As String = TextBixcomment.Text.Trim()
			Dim strname As String = ""
			Dim extension As String = ""
			Dim getADPOST As String = ""
			Dim str As String = ConfigurationManager.ConnectionStrings("DB").ConnectionString
			If FileUploadpost.HasFile Then
				strname = FileUploadpost.FileName.ToString()
				extension = System.IO.Path.GetExtension(strname)
				Using con As New SqlConnection(str)
					Dim cmd As New SqlCommand()
					' Image extension.
					If extension.ToLower() = ".jpg" OrElse extension.ToLower() = ".png" OrElse extension.ToLower() = ".gif" OrElse extension.ToLower() = ".jpeg" Then
						getADPOST = "Insert INTO File (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)"
						cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim())
						cmd.Parameters.AddWithValue("@UserName", Session("userName"))
						cmd.Parameters.AddWithValue("@ImageName1", strname)
					' Video extension.
					ElseIf extension.ToLower() = ".mp4" OrElse extension.ToLower() = ".wmv" OrElse extension.ToLower() = ".flv" OrElse extension.ToLower() = ".vob" OrElse extension.ToLower() = ".dat" Then
						getADPOST = "Insert INTO File (UserName,ContentPost,ImageName1) values (@UserName,@ContentPost,@ImageName1)"
						cmd.Parameters.AddWithValue("@ContentPost", TextBixcomment.Text.Replace(Environment.NewLine, "<br/>").Trim())
						cmd.Parameters.AddWithValue("@UserName", Session("userName"))
						cmd.Parameters.AddWithValue("@ImageName1", Convert.ToString("path/") & strname)
					End If
					cmd.CommandText = getADPOST
					cmd.CommandType = System.Data.CommandType.Text
					cmd.Connection = con
					con.Open()
					cmd.ExecuteNonQuery()
					con.Close()
					FileUploadpost.SaveAs(folderPath & Path.GetFileName(FileUploadpost.FileName))
					'save file from fileUpload to folder here.
					Dim message As String = "Post submitted successfully."
					ClientScript.RegisterStartupScript(Me.[GetType](), "Pop", (Convert.ToString("openModal5('") & message) + "');", True)
				End Using
			End If
		End If

		Me.DisplayVideo()
	End If
End Sub

Private Sub DisplayVideo()
	Dim str As String = ConfigurationManager.ConnectionStrings("DB").ConnectionString
	Dim con As New SqlConnection(str)
	Dim cmd As New SqlCommand("SELECT * FROM [File]", con)
	Dim sda As New SqlDataAdapter(cmd)
	Dim dt As New DataTable()
	sda.Fill(dt)
	Dim dt1 As DataTable = dt.Clone()
	For Each dr As DataRow In dt.Rows
		dt1.Rows.Add(dt.Rows(0)("Id"), Server.UrlEncode(dt.Rows(0)("FileName").ToString()))
	Next
	dlPost.DataSource = dt1
	dlPost.DataBind()
End Sub

Screenshot