您的位置:首页 > 编程语言 > ASP

黑马程序员--asp.net有关如何批量上传图片

2012-08-10 15:11 239 查看
---------------------- <a href="http://net.itheima.com/" target="blank">Windows Phone 7手机开发</a>、<a href="http://net.itheima.com/" target="blank">.Net培训</a>、期待与您交流! ----------------------

这是我最近总结的一些知识,我认为对于一些写这方面的人有帮助,就分享给大家看看,希望大家能用的到。

public partial class UpMultiFileControl2 : System.Web.UI.UserControl

{

protected void Page_Load(object sender, EventArgs e)

{

if (!Page.IsPostBack)

{

SaveCurrentPageFileControls();

}

}

protected void btAddFile_Click(object sender, EventArgs e)

{

AddOneFileControl();

}

/**//// <summary>

/// 添加一个上传文件控件

/// </summary>

private void AddOneFileControl()

{

ArrayList al = new ArrayList();

this.tbFiles.Rows.Clear();

GetFileControlsFromSession();

HtmlTableRow htr = new HtmlTableRow();

HtmlTableCell htc = new HtmlTableCell();

htc.Controls.Add(new FileUpload());

htr.Controls.Add(htc);

this.tbFiles.Rows.Add(htr);

SaveCurrentPageFileControls();

}

/**//// <summary>

/// 读取缓存中存储的上传文件控件集

/// </summary>

private void GetFileControlsFromSession()

{

ArrayList al = new ArrayList();

if (Session["FilesControls"] != null)

{

al = (System.Collections.ArrayList)Session["FilesControls"];

for (int i = 0; i < al.Count; i++)

{

HtmlTableRow htr1 = new HtmlTableRow();

HtmlTableCell htc1 = new HtmlTableCell();

htc1.Controls.Add((System.Web.UI.WebControls.FileUpload)al[i]);

htr1.Controls.Add(htc1);

this.tbFiles.Rows.Add(htr1);

}

}

}

/**//// <summary>

/// 保存当前页面上传文件控件集到缓存中

/// </summary>

private void SaveCurrentPageFileControls()

{

ArrayList al = new ArrayList();

foreach (Control controlTR in this.tbFiles.Controls)

{

if (controlTR.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")

{

HtmlTableCell htc = (HtmlTableCell)controlTR.Controls[0];

foreach (Control controlFileUpload in htc.Controls)

{

if (controlFileUpload.GetType().ToString() == "System.Web.UI.WebControls.FileUpload") {

FileUpload tempFileUpload = (FileUpload)controlFileUpload;

al.Add(tempFileUpload); }

} }

}

Session.Add("FilesControls", al);

}

protected void btUpFiles_Click(object sender, EventArgs e)

{

UpLoadFiles();

}

/**//// <summary>

/// 上传文件操作

/// </summary>

private void UpLoadFiles()

{

string filepath = Server.MapPath("./")+"UploadFiles";

HttpFileCollection uploadedFiles = Request.Files;

for (int i = 0; i < uploadedFiles.Count; i++)

{

HttpPostedFile userPostedFile = uploadedFiles[i];

try

{

if (userPostedFile.ContentLength > 0 )

{

userPostedFile.SaveAs(filepath + "\\" + System.IO.Path.GetFileName(userPostedFile.FileName));

Response.Write("已上传文件: \"" + filepath +"\\"+ userPostedFile.FileName +"\" <br> <br>" );

}

}

catch

{

Response.Write("上传文件: \"" + userPostedFile.FileName +"\"出错!");

}

}

if (Session["FilesControls"] != null)

{

Session.Remove("FilesControls");

}

}

}

(2). 改变上传文件大小和时间限制

<httpRuntime>

executionTimeout="110" //上传等待时间

maxRequestLength="4096" //上传文件大小,默认为4M

</httpRuntime>

上传文件大小是由上面两个参数所决定的. 涉及到安全因素,不要设得太大.

网上找的.

注意:页面上初始的file控件必须有runat="server"标志。也就是说,这个页面上必须至少有一个runat="server"的file控件,否则后台接收不到Request.Files。

<FORM id="form1" runat="server">

<DIV id="div1">

<INPUT ID="File1" TYPE="file" NAME="File1" runat="server">

<INPUT TYPE="button" VALUE="添加附件" onclick="javascript:AddFile();">

<INPUT TYPE="button" VALUE="删除附件" onclick="javascript:RemoveFile();">

<ASP:LISTBOX id="ListBox1" Width="200px" Height="100px" runat="server"> </ASP:LISTBOX>

<ASP:BUTTON id="Button1" runat="server" Text="保存" Width="60px"> </ASP:BUTTON>

</DIV>

<ASP:LITERAL ID="lResult" Runat="server"> </ASP:LITERAL>

</FORM>

<SCRIPT language="javascript">

<!--

function AddFile()

{

var file = document.getElementById("div1").firstChild;

if(file.value == "")

{

alert("请选择文件!");

return;

}

var ary = file.value.split("\");

var filename = ary[ary.length-1];

var bAddFile = true;

if(CheckOptionsExists(filename,document.getElementById("ListBox1")))

{

alert("文件已经存在列表中!");

div1.removeChild(file);

bAddFile = false;

}

var f = document.createElement("input");

f.type = "file";

f.name = "file"

div1.insertBefore(f,div1.firstChild);

if(!bAddFile)

{

return

}

var o = new Option();

o.innerText = filename;

o.value = file.uniqueID;

document.getElementById("ListBox1").appendChild(o);

file.style.display = "none";

}

function RemoveFile()

{

var lst = document.getElementById("ListBox1");

if(lst.selectedIndex == -1)

{

alert("请选择要删除的附件!");

return;

}

var id = lst.value;

div1.removeChild(document.all[id]);

lst.removeChild(lst.options[lst.selectedIndex]);

div1.firstChild.style.display = "";

}

//检查选项是否存在.

function CheckOptionsExists(value,ddl)

{

for(var i=0;i <ddl.options.length;i++)

{

if(ddl.options[i].innerText == value)

{

return true;

}

}

return false;

}

//-->

</SCRIPT>

后台代码就比较简单了。没有过多的处理,只是一个简单的保存。

private void Button1_Click(object sender, System.EventArgs e)

{

for(int i=0;i <Request.Files.Count;i++)

{

if(Request.Files[i].ContentLength >0)

{

string filename = System.IO.Path.GetFileName(Request.Files[i].FileName);

Request.Files[i].SaveAs(Server.MapPath(filename));

this.ListBox1.Items.Add(new ListItem(filename,filename));

}

this.lResult.Text = "保存成功!";

}

}

---------------------- <a href="http://net.itheima.com/" target="blank">Windows Phone 7手机开发</a>、<a href="http://net.itheima.com/" target="blank">.Net培训</a>、期待与您交流! ----------------------
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: