Search...

Wednesday, February 15, 2012

How to upload videos up to 20MB or more in asp.net using C#




********************************************************************************

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Upload Video Page</title>
</head>
<body>
    <form id="form1" runat="server" enctype="multipart/form-data">
    <div>
    <h1>Upload Video up to 20MB</h1>
    </div>
    <div>
        <input id="myfile" type="file" runat="server" />
        <asp:Button ID="butUpload" runat="server" onclick="butUpload_Click" Text="Upload" />
    </div>
    <div id="result" runat="server"></div>
    </form>
</body>
</html>

********************************************************************************

using System;
using System.IO;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
  
    protected void Page_Load(object sender, EventArgs e)
    {
      butUpload.Click+=new EventHandler(butUpload_Click);

    }
    protected void butUpload_Click(object sender, EventArgs e)
    {
        try
        {
            result.InnerHtml = "Uploading video please wait.....";
            if (myfile.PostedFile != null)
            {
            HttpPostedFile httpFile = myfile.PostedFile;
            string strPath = Server.MapPath("~/Video/" + Path.GetFileName(httpFile.FileName));
            int fileLength = httpFile.ContentLength;

            Byte[] myData = new Byte[fileLength];
            httpFile.InputStream.Read(myData, 0, fileLength);

                using (FileStream newFile = new FileStream(strPath, FileMode.Create))
                {
                    newFile.Write(myData, 0, myData.Length);
                }
            }
            else
            {
                result.InnerHtml = "Please upload video up to 20MB.";
            }
        }
        catch (Exception ex)
        {
            result.InnerHtml=ex.Message.ToString();
        }
    }
}

*********************************************************************************

/*Edit your web.config file*/

<?xml version="1.0"?>
<configuration>
<!--Upload 20*1024 MB-->
<location path="~/Video">
<system.web>
<httpRuntime executionTimeout="500" maxRequestLength="20480"/>
</system.web>
</location>
</configuration>

No comments: