文件上传:
界面:
logs.com/zt102545/admin/fb1a1abb-ec20-4f04-82fb-68ea7523576c_0_
files/283079258.png" alt="" />
前台代码:
<body style="text-align: center; background-image: url(Images/bg6.bmp);">
<form id="form" method="post" runat="server" enctype="multipart/form-data">
<p style="text-align: center">
</p>
<div>
<div style="text-align: center">
<br />
<table border="0" cellpadding="0" cellspacing="0" style="background-image: url(Images/wjsc.bmp); width: 412px; height: 293px">
<tr>
<td style="width: 105px; text-align: center">
<div style="text-align: center">
<div style="text-align: center">
<table cellpadding="0" cellspacing="0" style="width: 410px; height: 177px; text-align: center;">
<tr>
<td align="left" style="width: 100px; height: 42px; text-align: left"></td>
</tr>
<tr>
<td align="left" style="width: 100px; height: 30px; text-align: center"></td>
</tr>
<tr>
<td align="left" style="width: 100px; height: 37px; text-align: center">
<input type="file" name="file" style="width: 370px" id="File1" language="javascript" onclick="return File1_onclick()" runat="server" /></td>
</tr>
<tr>
<td align="left" style="width: 100px; height: 30px; text-align: center">
<asp:Label ID="lbltishi" runat="server" Font-Bold="False" Font-Size="9pt" ForeColor="Red"
Height="20px" Width="355px" Text="提示:请您选择要上传的文件"></asp:Label></td>
</tr>
<tr>
<td style="width: 100px; height: 30px; text-align: center;">
<asp:Button runat="server" Text="上传文件" ID="BtnUp" OnClick="BtnUp_Click" Width="128px" SkinID="btnSkin"></asp:Button></td>
</tr>
<tr>
<td style="width: 100px; height: 21px"> </td>
</tr>
</table>
</div>
</div>
</td>
</tr>
</table>
</div>
</div>
</form>
</body>
后台代码:
//上传方法
public void Up(int id)
{
HttpFileCollection UpLoad = HttpContext.Current.Request.Files; //获取页面所有上传文件
//实现多个文件上传,逐个获取
for (int i = 0; i < UpLoad.Count; i++)
{
HttpPostedFile file = UpLoad[i];//HttpPostedFile 类提供用于获取关于单独的文件的信息和读取及保存文件的属性和方法。
string fileName = Path.GetFileName(file.FileName);//获取页面上传文件的文件名
if (fileName != null)
{
file.SaveAs(MapPath("mr/") + fileName);//保存上传内容
directory.Insert(fileName, id, file.ContentLength, "mr/" + fileName, file.ContentType);//插入数据方法
}
}
Response.Write("<script>alert('恭喜您!文件上传成功!');window.close();</script>");
}
//上传按钮
protected void BtnUp_Click(object sender, EventArgs e)
{
if (this.File1.PostedFile.FileName == "")
{
Response.Write("<script>alert('很遗憾,上传文件不能为空!')</script>");
}
else
{
Up(ID1);
}
}
//插入数据(directory.Insert)
public int Insert(string mrming, int id, int nsize, string path, string s)
{
SqlConnection con = new SqlConnection(GetConStr());
SqlCommand cmd = new SqlCommand("procInsert", con);
cmd.CommandType = CommandType.StoredProcedure;
///添加存储过程的参数
SqlParameter pmrming = new SqlParameter("@mrming", SqlDbType.VarChar, 200);
pmrming.Value = mrming;
cmd.Parameters.Add(pmrming);
SqlParameter pmrid = new SqlParameter("@mrid", SqlDbType.Int, 4);
pmrid.Value = id;
cmd.Parameters.Add(pmrid);
SqlParameter psize = new SqlParameter("@size", SqlDbType.Int, 4);
psize.Value = nsize;
cmd.Parameters.Add(psize);
SqlParameter path1 = new SqlParameter("@path", SqlDbType.VarChar, 255);
path1.Value = path;
cmd.Parameters.Add(path1);
SqlParameter leibie1 = new SqlParameter("@Leibie", SqlDbType.VarChar, 200);
leibie1.Value = s;
cmd.Parameters.Add(leibie1);
int i = -1;
con.Open();
i = cmd.ExecuteNonQuery();
con.Close();
return i;
}
下载模块:
protected void rpZiYuan_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "downLoad") //下载
{
try
{
sql = "update T_ZiYuan set F_DownloadTimes =F_DownloadTimes+1 where F_Id=" + e.CommandArgument;
cs.ExecuteUpdate(sql); //更新下载次数
sql = "select * from T_ZiYuan where F_Id=" + e.CommandArgument;
DataRow row = cs.GetDataSet(sql).Tables[0].Rows[0];
string filePath = row["F_ZiYuanPath"].ToString();
filePath = Server.MapPath("~/ZiYuan/" + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open); //建立文件流
long fileSize = fs.Length;
Context.Response.ContentType = "application/octet-stream"; //设置输出流的 HTTP MIME 类型为application/octet-stream
Context.Response.AddHeader("Content-Disposition", "attachment;fileName=\"" + HttpUtility.UrlEncode(Path.GetFileName(filePath), System.Text.Encoding.UTF8) + "\"");//将 HTTP 头添加到输出流
Context.Response.AddHeader("Content-Length", fileSize.ToString());//增加报文头
byte[] fileBuffer = new byte[fileSize];
fs.Read(fileBuffer, 0, (int)fileSize);
fs.Close();
Context.Response.BinaryWrite(fileBuffer);
Context.Response.End();
//Response.Redirect("DownLoad.aspx");
}
catch
{
throw new Exception("对不起,文件下载错误!");
}
}
}