您的位置:首页 > 数据库

文件上传控件Fileupload(实现文件上传并写入数据库)

2015-11-22 12:58 387 查看
首先我们来说一下Fileupload这个文件上传控件的几大败笔:

1.上传之后按F5刷新,重复提交

2.提交以后按后退键Fileupload中的信息还在

3.不支持多文件上传

4.上传前不能检测文件大小

解决方法:

1.建立iframe在子页面实现或者重定向语句(Response.Redirect(Request.RawUrl);)

2.用JavaScript语句将fileupload中的val信息清空

4.利用js代码:fileupload.files[0].size代码解决,但需要浏览器支持html5

另外,需要强调的是如果要更改默认上传文件的最大值以及上传时间需要去web.config文件去修改

<httpRuntime maxRequestLength="8192" executionTimeout="90"/>

下面是利用iframe实现文件上传并写入数据库的例程:

iframe.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="iframe_upload.aspx.cs" Inherits="uploadfiles_iframe_upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<h2>upload上传文件保存到数据库</h2>
<iframe frameborder="0" width="100%" src="i_upload.aspx"></iframe>
</div>
</form>
</body>
</html>


i_upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="i_upload.aspx.cs" Inherits="uploadfiles_iframe_upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="../jquery-1.11.3.min.js" type="text/javascript"></script>
<title></title>
<script type="text/javascript">
$(document).ready(function () {
$("#fup1").val(""); //将fileupload中的val信息清空
});

function checkdata() {
if ($("#fup1").val() == "") {
alert("请选择文件");
$("#fup1").focus(); //将焦点定在fileupload控件上
return false;
}

var _file = document.getElementById("fup1"); //寻找页面中特定ID值的控件
var _size = _file.files[0].size; //在提交之前提取文件的大小

if (_size > 4000000) {
alert("文件过大");
$("#fup1").focus();
return false;
}

return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>

<asp:FileUpload ID="fup1" runat="server" />

<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" OnClientClick="return checkdata()" />

</div>
</form>
</body>
</html>


i_upload.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class uploadfiles_iframe_upload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}
protected void Button1_Click(object sender, EventArgs e)
{
Session["_tempFiles"] = fup1.PostedFile; //将信息写入session
Response.Redirect("i_info.aspx"); //转到i_info页面
}
}


i_info.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="i_info.aspx.cs" Inherits="uploadfiles_iframe_upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
title:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
fc_id:<asp:Label ID="lb_fcid" runat="server" Text="Label"></asp:Label><br/>
fc_name:<asp:Label ID="lb_fcname" runat="server" Text="Label"></asp:Label><br/>
targDir:<asp:Label ID="lb_tdir" runat="server" Text="Label"></asp:Label><br/>
targPath:<asp:Label ID="lb_fcpath" runat="server" Text="Label"></asp:Label><br />
</div>
<asp:Button ID="Button1" runat="server" Text="保存" onclick="Button1_Click" />
</form>
</body>
</html>


i_info.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Data.OleDb;

public partial class uploadfiles_iframe_upload : System.Web.UI.Page
{

string str_cnn = "Provider=MicroSoft.Jet.OLEDB.4.0;Data Source=";
string str_sourcefile = "~/uploadfiles/data.mdb";
OleDbCommand cmd;
OleDbConnection cnn;
OleDbDataReader deter;
string str_sql;

protected void Page_Load(object sender, EventArgs e)
{
if (Session["_tempFiles"] == null)
{
Response.Redirect("i_upload.aspx");
}
HttpPostedFile _myfiles = (HttpPostedFile)Session["_tempFiles"]; //读取session,存入_myfiles中
string _ext = Path.GetExtension(_myfiles.FileName).ToLower(); //获取文件的扩展名,并转化为小写

string str_conn = str_cnn + MapPath(str_sourcefile);
cnn = new OleDbConnection(str_conn);

cnn.Open();
//设置默认值
lb_fcid.Text = "1";
lb_fcname.Text = "其他";
lb_tdir.Text = "~/uploadfiles/其他";

str_sql = "SELECT * FROM T_FILE WHERE fc_ext like '%" + _ext + "%'";//注意变量值的分离方法
cmd = new OleDbCommand(str_sql, cnn);
deter = cmd.ExecuteReader();
<span style="white-space:pre">	</span>//读数据库
while (deter.Read())
{
lb_fcid.Text = deter["ID"].ToString();
lb_fcname.Text = deter["fc_name"].ToString();
lb_tdir.Text = deter["fc_path"].ToString();
}

TextBox1.Text = _myfiles.FileName.ToString();
lb_fcpath.Text = MapPath(lb_tdir.Text) + DateTime.Now.ToOADate() + _ext; //构建文件路径

cnn.Close();

}
protected void Button1_Click(object sender, EventArgs e)
{
string _targPath = lb_fcpath.Text;
string _targDir = lb_tdir.Text;

try
{
Directory.CreateDirectory(MapPath(_targDir)); //检查有无文件目录,如果没有则创建
((HttpPostedFile)Session["_tempfiles"]).SaveAs(_targPath);//存文件

}
catch { }

if (File.Exists(_targPath))
{
string _fname, _fcid, _title;
_fname = Path.GetFileName(lb_fcpath.Text);
_fcid = lb_fcid.Text;
_title = TextBox1.Text;

string str_conn = str_cnn + MapPath(str_sourcefile);
cnn = new OleDbConnection(str_conn);

cnn.Open();
<span style="white-space:pre">	</span>//存入数据库
str_sql = "INSERT INTO T_FILEINFO(f_name,f_path) values "+"('"+_fname+"','"+_title+"')";
cmd = new OleDbCommand(str_sql, cnn);
int i = cmd.ExecuteNonQuery();
cnn.Close();

if (i > 0)
{
Session.Remove("_tempfiles");//清除session
Response.Redirect("i_down.aspx");
}
}
}
}


i_down.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="i_down.aspx.cs" Inherits="uploadfiles_iframe_upload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<p>上传成功</p>
<p>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/uploadfiles/i_upload.aspx">再次上传</asp:HyperLink>
</p>

</div>
</form>
</body>
</html>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: