文件添加的方法:
public int AddFile(string title, string url, string type, int size)
{
string connectionString = ConfigurationManager.ConnectionStrings["SQLCONNECTIONSTRING"].ConnectionString;
SqlConnection conn = new SqlConnection(connectionString);
string cmdText = "Insert into [files] (F_Title,F_Url,F_Type,F_Size,F_CreatDate) values (@Title,@Url,@Type,@Size,GETDATE())";
SqlCommand cmd = new SqlCommand(cmdText, conn);
cmd.Parameters.Add("@Title", SqlDbType.VarChar, 200);
cmd.Parameters.Add("@Url", SqlDbType.VarChar, 255);
cmd.Parameters.Add("@Type", SqlDbType.VarChar, 50);
cmd.Parameters.Add("@Size", SqlDbType.Int, 4);
cmd.Parameters[0].Value = title;
cmd.Parameters[1].Value = url;
cmd.Parameters[2].Value = type;
cmd.Parameters[3].Value = size;
int result = -1;
try
{
conn.Open();
result = cmd.ExecuteNonQuery();
}
catch (Exception ex)
{
throw new Exception(ex.Message, ex);
}
finally
{
conn.Close();
}
return result;
}
简单的页面:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="UploadFiles.aspx.cs" Inherits="UploadFiles" %>
<!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>
<script language="javascript" type="text/javascript">
function addFile(max)
{
var file = document.getElementsByName("File");
if(file.length==1 && file[0].disabled==true)
{
file[0].disabled = false;
return;
}
if(file.length<max)
{
var fileButton = '<br /><input type="file" size="50" name="File" />';
document.getElementById("FileList").insertAdjacentHTML("beforeEnd",fileButton);
}
}
</script>
</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<div>
<table style="width: 400px; height: 182px">
<tr>
<td style="width: 57px; height: 90px" valign="top">
选择文件:</td>
<td style="width: 265px; height: 90px" valign="top">
<p id="FileList"><input type="file" disabled="disabled" size="50" name="File" /></p>
<input type="button" value="增加一个文件" onclick="addFile(<%=MaxFileCount %>)" /><font color="red">(最多上传<%=MaxFileCount%>个文件)</font>
</tr>
<tr>
<td style="width: 57px">
验证码:</td>
<td style="width: 265px">
<asp:TextBox ID="txtCode" runat="server" Height="20px" Width="160px"></asp:TextBox>
<asp:Image ID="Image1" runat="server" Height="21px" ImageUrl="~/ValidateImage.aspx"
Width="88px" /></td>
</tr>
<tr>
<td style="width: 57px">
</td>
<td style="width: 265px">
<asp:Button ID="btnsubmit" runat="server" Text="提交" OnClick="btnsubmit_Click" />
<asp:Label ID="lbMessage" runat="server"></asp:Label></td>
</tr>
</table>
</div>
</form>
</body>
</html>
页面背后的代码:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using ASPNETAJAXWeb.AjaxFileImage;
public partial class UploadFiles : System.Web.UI.Page
{
protected int MaxFileCount=AjaxFileImageSystem.MaxFileCount;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnsubmit_Click(object sender, EventArgs e)
{
if (Session["checkcode"] == null) return;
if (txtCode.Text != Session["checkcode"].ToString())
{
Response.Write("<script>alert('验证码不正确!')</script>"); return;
}
//获取文件列表中的每一个文件
HttpFileCollection fileList = HttpContext.Current.Request.Files;
if (fileList == null) return;
FileImage file = new FileImage();
int count = 0;
try
{
for (int i = 0; i < fileList.Count; i++)
{
//获取当前的文件
HttpPostedFile postedFile = fileList[i];
if (postedFile == null) continue;
///获取文件的文件名
string fileName = Path.GetFileNameWithoutExtension(postedFile.FileName);
string extension = Path.GetExtension(postedFile.FileName);
if (string.IsNullOrEmpty(extension) == true) continue;
//判断文件是否合法
bool isAllow = false;
foreach (string ext in AjaxFileImageSystem.AllowFileList)
{
if (ext == extension.ToLower())
{
isAllow = true; break;
}
}
if (isAllow == false) continue;
string timeFilename = AjaxFileImageSystem.CreateDateTimeString();
string storeUrl = timeFilename + extension;
string url = AjaxFileImageSystem.StoreFilePath + storeUrl;
string fullPath = Server.MapPath(url);
postedFile.SaveAs(fullPath);
file.AddFile(fileName, storeUrl, postedFile.ContentType, postedFile.ContentLength);
count++;
}
Response.Write("<script>alert('" + count + "个上传文件成功!');</script>");
}
catch (Exception ex)
{
lbMessage.Text = "上传文件失败,错误原因:" + ex.Message;
return;
}
// Response.Redirect("UploadFiles.aspx");
}
}
其中:另有一个图片生成类和一个上传设置类没给出