Asp.Net 音频文件上传和播放代码
程序员文章站
2024-03-07 18:34:46
一、音频文件上传和播放 在网上找到一个名叫aspnetpager的第三方控件,将aspnetpager.dll文件引用到项目的bin中。在网页中可直接调用。引用命名空间 u...
一、音频文件上传和播放
在网上找到一个名叫aspnetpager的第三方控件,将aspnetpager.dll文件引用到项目的bin中。在网页中可直接调用。引用命名空间 using telerik.webcontrols;
>>上传
protected void button1_click(object sender, eventargs e)
{
foreach (uploadedfile file in raduploadcontext.current.uploadedfiles)
{
if (file.filename != null && file.filename != "")
{
string strtime = memberid.tostring() + "-" + datetime.now.year.tostring() + datetime.now.month.tostring() + datetime.now.day.tostring() + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
string namefile = file.filename;//获取文件原来的名字
string namelast = namefile.substring(namefile.lastindexof("."));//获取文件的后缀名,lastindexof()-返回括号内字符的所在位置
string filepath = system.appdomain.currentdomain.basedirectory + @"savefilepath/mbmusic/";//文件上传的路径
system.io.fileinfo fi = new system.io.fileinfo(file.filename);
if (fi.extension.tolower() == ".mp3" || fi.extension.tolower() == ".wma" || fi.extension.tolower() == ".rm" || fi.extension.tolower() == ".wav")
{
string name = strtime + namelast;//文件最后保存的名字
mv = "http://www.cnblogs.com/savefilepath/mbmusic/" + name;
filesizes = int.parse(file.contentlength.tostring());
string path = filepath + name;
if (file.exists(path) == false)
{
file.saveas(filepath + name);
mf.mf_path = mv;
//mf.mf_path = mv;
}
else if (file.exists(path) == true)
{
name = strtime + "(" + tt + ")" + namelast;
file.saveas(filepath + name);
mv = "http://www.cnblogs.com/savefilepath/mbmusic/" + name;
mf.mf_path = mv;
//mf.mf_path = mv;
}
if (remainbytesize >= filesizes)
{
try
{
//将数据插入数据库
if (addnum > 0 && udpsize > 0)
{
//response.write("<script>alert('上传成功!!')</script>");
//bind();
}
else
{
response.write("<script>alert('上传失败!!')</script>");
return;
}
num++;
}
catch { }
}
else
{
response.write("<script>alert('空间不足,请申请空间!!')</script>");
return;
}
}
else
{
response.write("<script>alert('上传文件格式错误! 文件格式为mp3,wma,wav.rm的视频!')</script>");
return;
}
}
else
{
response.write("<script>alert('请选择上传的文件')</script>");
return;
}
}
}
>>播放
1、在播放页面添加控件literal1 控件
2、在页面后台添加调用音频播放器代码
public static string getflashtext(string url)
{
string str = @"<object id='mediaplayer' classid='clsid:6bf52a52-394a-11d3-b153-00c04f79faa6' height='62' width='406'>
<param name='rate' value='1' />
<param name='balance' value='0' />
<param name='currentposition' value='0' />
<param name='defaultframe' value='' />
<param name='playcount' value='1' />
<param name='autostart' value='-1' />
<param name='currentmarker' value='0' />
<param name='invokeurls' value='-1' />
<param name='baseurl' value='' />
<param name='volume' value='2' />
<param name='mute' value='0' />
<param name='uimode' value='full' />
<param name='stretchtofit' value='0' />
<param name='windowlessvideo' value='0' />
<param name='enabled' value='-1' />
<param name='enablecontextmenu' value='0' />
<param name='fullscreen' value='0' />
<param name='samistyle' value='' />
<param name='samilang' value='' />
<param name='samifilename' value='' />
<param name='captioningid' value='' />
<param name='enableerrordialogs' value='0' />
<param name='_cx' value='10742' />
<param name='_cy' value='1799' />
<param name='url' value='"+url+"'/></object>";
return str;
}
3、调用getflashtext()方法
public void bindmusic()
{
if (mfid != 0)
{
dataset dst = mf.search(mfid);
if (dst != null && dst.tables[0].rows.count > 0)
{
string url = dst.tables[0].rows[0]["mf_path"].tostring();
string[] strs = url.split(new char[] { '/' });
string mvname = strs[strs.length - 1];
string url1 = "savefilepath/mbmv/" + mvname;
literal1.text = getflashtext(url);//literal1调用方法播放音频
label1.text = dst.tables[0].rows[0]["mf_filename"].tostring();
//txt_filename.text = dst.tables[0].rows[0]["mf_filename"].tostring();
//txt_introduction.text = dst.tables[0].rows[0]["mf_introduction"].tostring();
}
}
}
在网上找到一个名叫aspnetpager的第三方控件,将aspnetpager.dll文件引用到项目的bin中。在网页中可直接调用。引用命名空间 using telerik.webcontrols;
>>上传
复制代码 代码如下:
protected void button1_click(object sender, eventargs e)
{
foreach (uploadedfile file in raduploadcontext.current.uploadedfiles)
{
if (file.filename != null && file.filename != "")
{
string strtime = memberid.tostring() + "-" + datetime.now.year.tostring() + datetime.now.month.tostring() + datetime.now.day.tostring() + datetime.now.hour.tostring() + datetime.now.minute.tostring() + datetime.now.millisecond.tostring();
string namefile = file.filename;//获取文件原来的名字
string namelast = namefile.substring(namefile.lastindexof("."));//获取文件的后缀名,lastindexof()-返回括号内字符的所在位置
string filepath = system.appdomain.currentdomain.basedirectory + @"savefilepath/mbmusic/";//文件上传的路径
system.io.fileinfo fi = new system.io.fileinfo(file.filename);
if (fi.extension.tolower() == ".mp3" || fi.extension.tolower() == ".wma" || fi.extension.tolower() == ".rm" || fi.extension.tolower() == ".wav")
{
string name = strtime + namelast;//文件最后保存的名字
mv = "http://www.cnblogs.com/savefilepath/mbmusic/" + name;
filesizes = int.parse(file.contentlength.tostring());
string path = filepath + name;
if (file.exists(path) == false)
{
file.saveas(filepath + name);
mf.mf_path = mv;
//mf.mf_path = mv;
}
else if (file.exists(path) == true)
{
name = strtime + "(" + tt + ")" + namelast;
file.saveas(filepath + name);
mv = "http://www.cnblogs.com/savefilepath/mbmusic/" + name;
mf.mf_path = mv;
//mf.mf_path = mv;
}
if (remainbytesize >= filesizes)
{
try
{
//将数据插入数据库
if (addnum > 0 && udpsize > 0)
{
//response.write("<script>alert('上传成功!!')</script>");
//bind();
}
else
{
response.write("<script>alert('上传失败!!')</script>");
return;
}
num++;
}
catch { }
}
else
{
response.write("<script>alert('空间不足,请申请空间!!')</script>");
return;
}
}
else
{
response.write("<script>alert('上传文件格式错误! 文件格式为mp3,wma,wav.rm的视频!')</script>");
return;
}
}
else
{
response.write("<script>alert('请选择上传的文件')</script>");
return;
}
}
}
>>播放
1、在播放页面添加控件literal1 控件
2、在页面后台添加调用音频播放器代码
复制代码 代码如下:
public static string getflashtext(string url)
{
string str = @"<object id='mediaplayer' classid='clsid:6bf52a52-394a-11d3-b153-00c04f79faa6' height='62' width='406'>
<param name='rate' value='1' />
<param name='balance' value='0' />
<param name='currentposition' value='0' />
<param name='defaultframe' value='' />
<param name='playcount' value='1' />
<param name='autostart' value='-1' />
<param name='currentmarker' value='0' />
<param name='invokeurls' value='-1' />
<param name='baseurl' value='' />
<param name='volume' value='2' />
<param name='mute' value='0' />
<param name='uimode' value='full' />
<param name='stretchtofit' value='0' />
<param name='windowlessvideo' value='0' />
<param name='enabled' value='-1' />
<param name='enablecontextmenu' value='0' />
<param name='fullscreen' value='0' />
<param name='samistyle' value='' />
<param name='samilang' value='' />
<param name='samifilename' value='' />
<param name='captioningid' value='' />
<param name='enableerrordialogs' value='0' />
<param name='_cx' value='10742' />
<param name='_cy' value='1799' />
<param name='url' value='"+url+"'/></object>";
return str;
}
3、调用getflashtext()方法
复制代码 代码如下:
public void bindmusic()
{
if (mfid != 0)
{
dataset dst = mf.search(mfid);
if (dst != null && dst.tables[0].rows.count > 0)
{
string url = dst.tables[0].rows[0]["mf_path"].tostring();
string[] strs = url.split(new char[] { '/' });
string mvname = strs[strs.length - 1];
string url1 = "savefilepath/mbmv/" + mvname;
literal1.text = getflashtext(url);//literal1调用方法播放音频
label1.text = dst.tables[0].rows[0]["mf_filename"].tostring();
//txt_filename.text = dst.tables[0].rows[0]["mf_filename"].tostring();
//txt_introduction.text = dst.tables[0].rows[0]["mf_introduction"].tostring();
}
}
}