---------------------- <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="添加附件" οnclick="javascript:AddFile();">
<INPUT TYPE="button" VALUE="刪除附件" οnclick="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>、期待與您交流! ----------------------