Friday, 18 May 2012

Upload Video files

Single video file upload:

protected void btnUploadVideofiles_Click(object sender, EventArgs e)
{
    if (FileUploadVideofiles.HasFile)
    {
        try
        {
            string filename = Path.GetFileName(FileUploadVideofiles.FileName);
            string fileExtension = Path.GetExtension(filename);
            if (fileExtension == ".wmv" || fileExtension == ".swf" || fileExtension == ".avi" || fileExtension == ".mp4" || fileExtension == ".mpeg" || fileExtension == ".mpg" || fileExtension == ".qt" || fileExtension == ".flv")
            {
                FileUploadVideofiles.SaveAs(Server.MapPath(@"~/Upload/Media/") + filename);
                string filePath = @"~/Upload/Media/" + filename;
                lblMessage.Text = "File is uploaded!";
            }
            else
            {
                lblMessage.Text = "Please Upload videos file only!";
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

Multiple video upload

protected void btnUploadmultiVideofiles_Click(object sender, EventArgs e)
{
    if (FileUploadVideofiles.HasFile)
    {
        try
        {
            // Get the HttpFileCollection
            HttpFileCollection uploadedVideoFiles = Request.Files;
            for (int i = 0; i < uploadedVideoFiles.Count; i++)
            {
                HttpPostedFile hpfiles = uploadedVideoFiles[i];
                if (hpfiles.ContentLength > 0)
                {
                    hpfiles.SaveAs(Server.MapPath("~/Upload/Media/") + Path.GetFileName(hpfiles.FileName));
                }
            }
        }
        catch (Exception ex)
        {
            Lblmessage2.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
        }
    }
}

Html code

<div>
    <fieldset>
    <legend>Upload Videos Files</legend>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <Triggers >
                <asp:PostBackTrigger ControlID ="btnUploadVideofiles" />
            </Triggers>
            <ContentTemplate >
               <asp:FileUpload ID="FileUploadVideofiles" runat="server" />
               <asp:Button ID="btnUploadVideofiles" runat="server" Text="Upload Video Files" onclick="btnUploadVideofiles_Click" />
               <asp:Label ID="lblMessage" runat="server" ForeColor="#3366FF"></asp:Label>
                <asp:ScriptManager ID="ScriptManager1" runat="server">
                </asp:ScriptManager>
            </ContentTemplate>
        </asp:UpdatePanel>
        <div>
            <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
                Text="Upload More Videos" />
        </div>
        <asp:Panel ID="Panel1" runat="server" Visible="false">
        <asp:UpdatePanel ID="UpdatePanel2" runat="server" Visible="true">
            <Triggers >
                <asp:PostBackTrigger ControlID ="btnUploadmultiVideofiles" />
            </Triggers>
            <ContentTemplate >
               <asp:FileUpload ID="FileUpload2" runat="server" />
               <asp:Button ID="btnUploadMultiVideofiles" runat="server" Text="Upload Multi Video Files" onclick="btnUploadmultiVideofiles_Click" />
               <asp:Label ID="Lblmessage2" runat="server" ForeColor="#3366FF"></asp:Label>
               
                </ContentTemplate>
        </asp:UpdatePanel>
        </asp:Panel>   
  </fieldset>
    </div>


No comments:

Post a Comment