Posted: 6/15/2011
I am a beginer of asp.Net and i need a code to upload a video...
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
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)
Gjorgi & priyanshu, nice answers guys!
Keep up the good work!
Posted: 8/8/2011
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)