سلام
----یه مقدمه اول (در مورد بهینه ترین روش ذخیره سازی دیتا)
یه روایتی هست که می گه اگه سایز فایلهاتون کوچیک بود (مثل عکسها اون هم نه با رزولوشن بالا و صوت ها و ویدیو های چند ثانیه ای) اونها رو تو database ذخیره کنید. اما اگه سایز زیادی داشتن اونها رو (تو پروژه های نت) تو سرور آپلود کنید و مسیرشون رو تو database ذخیره کنید
** این مثال برای فایلهای ویدئو آورده شده اما شما میتونین به همین طریق و با استفاده از همین type های فیلدهای database اون رو برای فایلهای صوتی استفاده کنید
**کدها به زبان C#.net نوشته شده اند و برای sql server 2005
**
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
namespaceهایی هستن که باید به پروژتون اضافه بشن
** این مثال برای پروژه های وب هست. اگه می خواین برای pc بنویسید با یه خورده دستکاری همچی رو براه میشه
--- فیلدهای مورد نیاز:
[LEFT]field name type
[LEFT]ID int
video varbinary(MAX)
Video_Name nvarchar(50)
Video_Size bigint
[RIGHT]--- استفاده از کد
آپلود کردن فایل در این کد برای اینه که اون رو به نوع bytes تبدیل کنیم. این کار رو توسط کلاس HTTPPostedFile انجام می دیم سپس تو یک فیلد در database از نوع varbinary قرار می دیم.
[LEFT]
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 u have set the type of video column to varbinary(MAX)
[RIGHT]--- حالا چطوری فایل رو تو player پخش کنیم؟!
مشکل اینجاست که تو player باید propertry ی src رو رو فایل تنظیم کنیم در حالی که ما جز یک متغیر از نوع bytes چیز دیگه یی نداریم!!! از 1 handler استفاده می کنیم
[LEFT]
using System;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
public class VideoHandler : IHttpHandler
{
public void ProcessRequest (HttpContext context)
{
string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;
SqlConnection connection = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("SELECT Video, Video_Name FROM Videos WHERE ID = @id", connection);
cmd.Parameters.Add("@id", SqlDbType.Int).Value = context.Request.QueryString["id"];
try
{
connection.Open();
SqlDataReader reader = cmd.ExecuteReader(CommandBehavior.Default);
if (reader.HasRows)
{
while (reader.Read())
{
context.Response.ContentType = reader["Video_Name"].ToString();
context.Response.BinaryWrite((byte[])reader["Video"]);
}
}
}
finally
{
connection.Close();
}
}
public bool IsReusable
{
get {
return false;
}
}
}
[RIGHT]همونطور که می بینید یک متغیر از نوع HttpContext داریم که توسط کلاس SqlDataReader متغیر bytes رو تو خودش می نویسه
حالا واسه پخش فایل باید از یک کنترل asp.net استفاده کنیم انجا از کنترل Repeter استفاده شده. با یک sql server, sql adapter رو می خونیم datasource رو با کنترل bind, Repeter می کنیم. اینجا میتونین انتخاب کنین که کدوم صوت یا ویدئوی database انتخاب بشه
[LEFT]
private DataTable GetSpecificVideo(object i)//pass the id of the video
{
string connectionString = ConfigurationManager.ConnectionStrings["uploadConnectionString"].ConnectionString;
SqlDataAdapter adapter = new SqlDataAdapter("SELECT Video, ID FROM Videos WHERE ID = @id", connectionString);
adapter.SelectCommand.Parameters.Add("@id", SqlDbType.Int).Value = (int)i;
DataTable table = new DataTable();
adapter.Fill(table);
return table;
}
protected void ButtonShowVideo_Click(object sender, EventArgs e)
{
Repeater1.DataSource = GetSpecificVideo(2);//the video id (2 is example)
Repeater1.DataBind();
}
[RIGHT]
حالا وقت player هستش
به property ی url مقدار <'%# "VideoHandler.ashx?id=" + Eval("ID") %'> رو میدیم, مقدار ID همون اسم سطون IDی Datasourceای هست که repeater به اون Bind شده.
[LEFT]
کد HTML:
<asp:Button ID="ButtonShowVideo" runat="server" onclick="ButtonShowVideo_Click"
Text="Show Video" />
/>
<asp:Repeater ID="Repeater1" runat="server">
<object id="player" classid="clsid:6BF52A52-394A-11D3-B153-00C04F79FAA6"
height="170" width="300">
name="url" value='<%# "VideoHandler.ashx?id=" + Eval("ID") %>'/> name="showcontrols" value="true" /> name="autostart" value="true" /> </object> </asp:Repeater>
[RIGHT]هوفف. از مدیرا خواهش می کنم که یه کاری کنن که زبان انگلیسی با پرانتز و ویرگول فارسی کنار بیاد.
و اینکه من نتونستم تگی برای کد های .net پیدا کنم. لطفاً اگه میدونید چطوریه با پیام خصوصی بهم بگید