Loading ...

please give the code for video upload

Who is online?  0 guests and 0 members
home  »  forums   »  asp.net topics   »  client side web development   » please give the code for video upload

please give the code for video upload

Posts under the topic: please give the code for video upload

Posted: 6/15/2011

Lurker 25  points  Lurker
  • Joined on: 6/15/2011
  • Posts: 5

I am a beginer of asp.Net  and i need a code to upload a video...


Posted: 6/15/2011

Starter 727  points  Starter
  • Joined on: 6/6/2011
  • Posts: 74
  Answered

Hi Tarumoy,

Please find the simple project on this link: 

Video Upload Control

In the sample you can find how to upload and insert files into SQL Server (specially audio, video, and image files) using C# and ADO.NET, and then how to show the video file in an ASP.NET page with a player control.

Because you are begginer, if something is too hard for you to understand, don't be shy to ask.

Best Regards,

Gjorgji Dimitrov


Posted: 6/15/2011

Lurker 90  points  Lurker
  • Joined on: 6/13/2011
  • Posts: 10
  Answered

this you can done by several ways

s u can use fileupload control for this and  put the restriction on the allowd file type

say for example fileupload control id is UploadFile than you can do this

 

put this in code view note:-assign the read write permissions to the folder to whom you want to save this file

//first check weither file is selected or not 

if (UploadFile.HasFile)

            {

                try

                {

                    path = Server.MapPath("./foldername/") +  "/" + UploadFile.FileName;

                    //save the file to the required folder

                    UploadFile.SaveAs(path);

                    Label1.Text = "filename:" + UploadFile.PostedFile.FileName + "<br>" + UploadFile.PostedFile.ContentLength + " bytes<br>" + "Content type:" + UploadFile.PostedFile.ContentType;

                }

}

Server.mappath will return a full path to the folder to whom  you want  to upload video also dont forget to concatenate the file name(UploadFile.FileName) 

 

with the above code you can upload any file but to restrict all other file types except some videos extensions do as follows 

in the design view just paste the below code

<asp:FileUpload ID="UploadFile" runat="server" />(this is you fileupload control code)

<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="UploadFile"

                                    ErrorMessage="must specify test image"></asp:RequiredFieldValidator>

<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="UploadTestFile" ErrorMessage="only files of type(wmv,avi) is accepted " ValidationExpression="[a-zA-Z\\].*(.wmv|.WMV|.avi|.AVI)$" ForeColor="#9966FF"></asp:RegularExpressionValidator>

             the above code will alert you 1) weither file is selected or not(asp:RequiredFieldValidator)

     2) allowed file extension  (asp:RegularExpressionValidator)    

 

2) if you want to restrict according to the file size than u can use this line
string filesize= UploadFile.PostedFile.ContentLength
if(filesize<2100000) this figure is in bytes approx 2m

 

 


Posted: 6/15/2011

Professional 8505  points  Professional
  • Joined on: 5/3/2010
  • Posts: 391

Gjorgi & priyanshu, nice answers guys!

Keep up the good work!


Posted: 8/8/2011

Lurker 15  points  Lurker
  • Joined on: 8/8/2011
  • Posts: 3

Hi..here i provides one more coding part for upload videos to database,

using System.IO;
using System.Data.SqlClient;

public partial class UploadVideo : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    byte[] buffer;
    //this is the array of bytes which will hold the data (file)

    SqlConnection connection;
    protected void ButtonUpload_Click(object sender, EventArgs e)
    {
        //check the file

        if (FileUpload1.HasFile && FileUpload1.PostedFile != null
            && FileUpload1.PostedFile.FileName != "")
        {
            HttpPostedFile file = FileUpload1.PostedFile;
            //retrieve the HttpPostedFile object

            buffer = new byte[file.ContentLength];
            int bytesReaded = file.InputStream.Read(buffer, 0,
                              FileUpload1.PostedFile.ContentLength);
            //the HttpPostedFile has InputStream porperty (using System.IO;)
            //which can read the stream to the buffer object,
            //the first parameter is the array of bytes to store in,
            //the second parameter is the zero index (of specific byte)
            //where to start storing in the buffer,
            //the third parameter is the number of bytes
            //you want to read (do u care about this?)

            if (bytesReaded > 0)
            {
                try
                {
                    string connectionString =
                      ConfigurationManager.ConnectionStrings[
                      "uploadConnectionString"].ConnectionString;
                    connection = new SqlConnection(connectionString);
                    SqlCommand cmd = new SqlCommand
                    ("INSERT INTO Videos (Video, Video_Name, Video_Size)" +
                     " VALUES (@video, @videoName, @videoSize)", connection);
                    cmd.Parameters.Add("@video",
                        SqlDbType.VarBinary, buffer.Length).Value = buffer;
                    cmd.Parameters.Add("@videoName",
                        SqlDbType.NVarChar).Value = FileUpload1.FileName;
                    cmd.Parameters.Add("@videoSize",
                        SqlDbType.BigInt).Value = file.ContentLength;
                    using (connection)
                    {
                        connection.Open();
                        int i = cmd.ExecuteNonQuery();
                        Label1.Text = "uploaded, " + i.ToString() + " rows affected";
                    }
                }
                catch (Exception ex)
                {
                    Label1.Text = ex.Message.ToString();
                }
            }

        }
        else
        {
            Label1.Text = "Choose a valid video file";
        }
    }
}
//create a sqlcommand object passing the query and the sqlconnection object
//when declaring the parameters you have to be sure
//you have set the type of video column to varbinary(MAX)

Page 1 of 1 (5 items)