- 实验目的
熟练掌握多媒体素材上传服务器的后台管理操作
- 实验内容
- 任务1:设计完成“我的相册影音库”前台页面;
- 任务2:后台实现“我的相册影音库”中上传图片、视频、音频等、查看、修改、删除、评论相册影音库中每个多媒体素材等基本功能。
- 实验环境
Microsoft Visual Studio 2019
- 实验步骤
任务1:设计完成“我的相册影音库”前台页面
设计文件上传页面的布局
HTML如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
<style type="text/css">
.auto-style1 {
width: 100%;
background-color: #C0C0C0;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<table align="center" class="auto-style1" style="width: 600px; height: 400px" >
<tr style="width: 600px; height: 100px" >
<td style="background-color: #0000FF; width:200px;height:100px">
<br />
</td>
<td style="background-color: #0000FF; width:400px;height:100px; font-size: x-large; color: #FF00FF; font-family: 幼圆;">
文件上传</td>
</tr>
<tr style="width: 600px; height: 100px">
<td style="width:200px;height:100px; font-family: 幼圆;">
文件类型:<br />
</td>
<td style="width:400px;height:100px">
<asp:DropDownList ID="dd1" runat="server" Height="40px" Width="225px">
<asp:ListItem>图片</asp:ListItem>
<asp:ListItem>MP3音频</asp:ListItem>
<asp:ListItem>MP4视频</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr style="width: 600px; height: 100px">
<td style="width:200px;height:100px; font-family: 幼圆;">
上传文件:<br />
</td>
<td style="width:400px;height:100px">
<asp:FileUpload ID="up1" runat="server" Height="40px" Width="334px" />
</td>
</tr>
<tr style="width: 600px; height: 100px">
<td style="width:200px;height:100px; font-family: 幼圆;">
上传者:</td>
<td style="width:400px;height:100px">
<asp:TextBox ID="txt1" runat="server" Height="40px" Width="208px" OnTextChanged="txt1_TextChanged"></asp:TextBox>
</td>
</tr>
<tr style="width: 600px; height: 100px">
<td style="width:200px;height:100px">
<asp:Label ID="lb1" runat="server" Font-Bold="True" Font-Size="Medium" ForeColor="#FF3300"></asp:Label>
<br />
</td>
<td style="width:400px;height:100px">
<asp:Button ID="btn1" runat="server" Height="58px" OnClick="btn1_Click" Text="上传" Width="165px" />
<asp:Button ID="btn2" runat="server" OnClick="btn2_Click" Text="查看上传列表" Visible="False" />
</td>
</tr>
</table>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource2">
<Columns>
<asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
<asp:BoundField DataField="riqi" HeaderText="riqi" SortExpression="riqi" />
<asp:BoundField DataField="wenjianming" HeaderText="wenjianming" SortExpression="wenjianming" />
<asp:BoundField DataField="leixing" HeaderText="leixing" SortExpression="leixing" />
<asp:HyperLinkField DataNavigateUrlFields="name,wenjianming" DataNavigateUrlFormatString="chakan.aspx?name={0}&wenjianming={1}" HeaderText="chakan" Text="查看" />
<asp:HyperLinkField DataNavigateUrlFields="name,wenjianming" DataNavigateUrlFormatString="del.aspx?name={0}&wenjianming={1}" HeaderText="shanchu" Text="删除" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:sy4ConnectionString %>" SelectCommand="SELECT * FROM [wenjian]"></asp:SqlDataSource>
</form>
</body>
</html>
- 上传按钮代码
protected void btn1_Click(object sender, EventArgs e)
{
//测试文件类型是否符合的变量
Boolean fileOK = false;
//设置服务器中保存文件的路径
String path = Server.MapPath("./up/");
//判断是否选择了文件
if (up1.HasFile)
{
//返回文件的扩展名
String fileExtension =
System.IO.Path.GetExtension(up1.FileName).ToLower();
//设置限定的文件类型
String[] allowedExtensions =
{ ".mp3", ".gif", ".jpg", ".mp4"};
//判断用户选择的文件类型是否受限
for (int i = 0; i < allowedExtensions.Length; i++)
{
if (fileExtension == allowedExtensions[i])
{
fileOK = true;
}
}
}
//如果文件大于100M,则不允许上传
if (up1.PostedFile.ContentLength > 102400000)
{
fileOK = false;
}
//如果文件类型符合
if (fileOK)
{
try
{
// 将文件保存到指定的文件夹下
up1.PostedFile.SaveAs(path + up1.FileName);
lb1.Text = "文件上传成功!" + up1.FileName.ToString();
}
catch
{
lb1.Text = "无法实现文件的上传。";
}
}
else
{
lb1.Text = "文件类型不对或文件超出100M。";
}
string name1, leixing, zuozhe;
name1 = up1.FileName.ToString();
leixing = dd1.SelectedItem.ToString();
zuozhe = txt1.Text.ToString();
if (Page.IsValid == true)
{
SqlConnection con = db.createconnection();
con.Open();
String cmdText = "insert into wenjian(wenjianming,leixing,name,riqi) values ( '" + name1 + "', '" + leixing + "','" + zuozhe + "',getdate())";
SqlCommand myCommand = new SqlCommand(cmdText, con);
myCommand.ExecuteNonQuery();
con.Close();
lb1.Visible = true;
lb1.Text = " 文件上传成功!";
btn2.Visible = true;
}
else
{
lb1.Visible = true;
lb1.Text = " 文件上传失败!";
}
}
- 查看上传列表按钮代码:
protected void btn2_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
}
- 任务2:后台实现“我的相册影音库”中上传图片、视频、音频等、查看、修改、删除、评论相册影音库中每个多媒体素材等基本功能。
- 查看页面HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="chakan.aspx.cs" Inherits="chakan" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<p>
<asp:Label ID="Label2" runat="server" Text="Label"></asp:Label>
</p>
<p>
</p>
<asp:TextBox ID="TextBox1" runat="server" Height="94px" Width="446px"></asp:TextBox>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="评论" />
</form>
</body>
</html>
- 查看按钮代码:
protected void Page_Load(object sender, EventArgs e)
{
string name1 = Request.QueryString["name"].ToString();
string name2 = Request.QueryString["wenjianming"].ToString();
Label1.Text = name1;
Label2.Text = "<embed src = '/up/" + name2 + "' width = '400' height = '300' id = 'music' autostart = 'true'></embed>";
}
- 删除页面HTML:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="del.aspx.cs" Inherits="del" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
<asp:Label ID="lb1" runat="server"></asp:Label>
<asp:Label ID="lb2" runat="server"></asp:Label>
</form>
</body>
</html>
- 删除按钮代码:
protected void Page_Load(object sender, EventArgs e)
{
string name1 = Request.QueryString["name"].ToString();
string name2 = Request.QueryString["wenjianming"].ToString();
SqlConnection con = db.createconnection();
String cmdText = "delete from wenjian where wenjianming='" + name1.ToString() + "'";
SqlCommand myCommand = new SqlCommand(cmdText, con);
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
Response.Redirect("Default.aspx");
}