日期:2014-05-19  浏览次数:20413 次

如何实现图片的批量上传?
小弟现在在做一个类似于网上相册的东西,想实现用户一次选择多个图片后,然后一次上传到服务器段的文件夹中,我需要把每个文件的路径存到数据库中,请问这中该怎么实现呢?

------解决方案--------------------
.多文件上传

效果图:http://p.blog.csdn.net/images/p_blog_csdn_net/21aspnet/286353/o_f2.gif


后台代码:
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;

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

}
protected void bt_upload_Click(object sender, EventArgs e)
{

if ((FileUpload1.PostedFile.FileName == " " && FileUpload2.PostedFile.FileName == " ")&&FileUpload3.PostedFile.FileName == " ")
{
this.lb_info.Text = "请选择文件! ";
}
else
{
HttpFileCollection myfiles = Request.Files;
for (int i = 0; i < myfiles.Count; i++)
{
HttpPostedFile mypost = myfiles[i];
try
{
if (mypost.ContentLength > 0)
{
string filepath = mypost.FileName;
string filename = filepath.Substring(filepath.LastIndexOf( "\\ ") + 1);
string serverpath = Server.MapPath( "images/ ") + filename;
mypost.SaveAs(serverpath);
this.lb_info.Text = "上传成功! ";
}
}
catch (Exception error)
{
this.lb_info.Text = "上传发生错误!原因: " + error.ToString();
}

}

}
}

}

前台代码:
<%@ Page Language= "C# " AutoEventWireup= "true " CodeFile= "Default.aspx.cs " Inherits= "_Default " %>

<!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> 多文件上传 清清月儿http://blog.csdn.net/21aspnet/ </title>
</head>
<body>
<form id= "form1 " runat= "server ">
<div>
<table style= "width: 343px ">
<tr>
<td style= "width: 100px ">
多文件上传 </td>
<td style= "width: 100px ">
</td>
</tr>
<tr>
<td style= "width: 100px ">
<asp:FileUpload ID= "FileUpload1 " runat= "server " Width= "475px " />
</td>
<td style= "width: 100px ">
</td>
</tr>