在我下面的代碼中,我試圖通過ASP.NET上傳文件。我動態創建FileUpload控件,這意味着它不在我的ViewState中(我認爲)意味着我不能使用控件上傳文件,除非我使用舊式multipat/form-data方式,而我不想做。我需要能夠允許用戶創建多個FileUpload字段,然後當他們單擊Upload File(s)按鈕時,它將遍歷所有FileUpload字段並將其上載到服務器。如何從ASP.NET動態創建的FileUpload控件上傳文件?
我確定有一種方法可以做到這一點,我只是沒有想到 - TIA!
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub btnAdd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim fup As New FileUpload()
fup.ID = "FileUpload1"
PlaceHolder1.Controls.Add(fup)
End Sub
Protected Sub btnUploadFile_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' HOW DO I GET THE FILE THAT WAS SELECTED IN THE DYNAMICALLY CREATE FILEUPLOAD CONTROL?
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div style="padding:13px">
<asp:Button ID="btnAdd" runat="server" Text="Add FileUpload Control" OnClick="btnAdd_Click" />
<br /><br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
<br /><br />
<asp:Button ID="btnUploadFile" runat="server" Text="Upload File(s)" OnClick="btnUploadFile_Click" />
</div>
</form>
</body>
</html>