通过以下代码动态加载三个fileupload控件。
protected void Page_Load(object sender, EventArgs e)
{
Table tblDocuments = new Table();
tblDocuments.CellPadding=8;
tblDocuments.CellSpacing=8;
TableRow tr7 = new TableRow();
TableCell tc13 = new TableCell();
Label lblSRV=new Label();
lblSRV.Text="SRV:";
tc13.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
lblSRV.ForeColor=System.Drawing.Color.Black;
lblSRV.Font.Bold=true;
tc13.Controls.Add(lblSRV);
tr7.Cells.Add(tc13);
TableCell tc14 = new TableCell();
fuUploadSRV.ID = "SRV";
tc14.Controls.Add(fuUploadSRV);
tc14.HorizontalAlign = HorizontalAlign.Center;
tc14.VerticalAlign = VerticalAlign.Middle;
tr7.Cells.Add(tc14);
tblDocuments.Rows.Add(tr7);
TableRow tr8 = new TableRow();
TableCell tc15 = new TableCell();
Label lblMAtrialCertificate = new Label();
lblMAtrialCertificate.Text = "Material/Product Certificate:";
lblMAtrialCertificate.ForeColor = System.Drawing.Color.Black;
lblMAtrialCertificate.Font.Bold = true;
tc15.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
tc15.Controls.Add(lblMAtrialCertificate);
tr8.Cells.Add(tc15);
TableCell tc16 = new TableCell();
fuUploadMaterialCertificate.ID = "MaterialCertificate";
tc16.Controls.Add(fuUploadMaterialCertificate);
tc16.HorizontalAlign = HorizontalAlign.Center;
tc16.VerticalAlign = VerticalAlign.Middle;
tr8.Cells.Add(tc16);
tblDocuments.Rows.Add(tr8);
TableRow tr9 = new TableRow();
TableCell tc17 = new TableCell();
Label lblPurchaseOrder = new Label();
lblPurchaseOrder.Text = "Purchase Order:";
lblPurchaseOrder.ForeColor = System.Drawing.Color.Black;
lblPurchaseOrder.Font.Bold = true;
tc17.BackColor = System.Drawing.ColorTranslator.FromHtml("#4FB0B0");
tc17.Controls.Add(lblPurchaseOrder);
tr9.Cells.Add(tc17);
TableCell tc18 = new TableCell();
fuUploadPurchaseOrder.ID="Purchase";
tc18.Controls.Add(fuUploadPurchaseOrder);
tc18.HorizontalAlign = HorizontalAlign.Center;
tc18.VerticalAlign = VerticalAlign.Middle;
tr9.Cells.Add(tc18);
tblDocuments.Rows.Add(tr9);
placeSubmit.Controls.Add(tblDocuments);
}
我在按钮点击事件中处理文件
void btnSubmit_Click(object sender, EventArgs e)
{
if (fuUploadSRV.HasFile)
{
string srvFilename = Path.GetFileName(fuUploadSRV.PostedFile.FileName);
Stream fs = fuUploadSRV.PostedFile.InputStream;
int filesize = fuUploadSRV.PostedFile.ContentLength;
BinaryReader br = new BinaryReader(fs);
byte[] content = br.ReadBytes(filesize);
Objects.UpdateSRVFileDetails(srvFilename, content,txtSRV.Text.ToString().Trim());
}
if (fuUploadMaterialCertificate.HasFile)
{
string materialFilename = Path.GetFileName(fuUploadMaterialCertificate.PostedFile.FileName);
Stream fs = fuUploadMaterialCertificate.PostedFile.InputStream;
int filesize = fuUploadMaterialCertificate.PostedFile.ContentLength;
BinaryReader br = new BinaryReader(fs);
byte[] content = br.ReadBytes(filesize);
Objects.UpdateMaterialCertificateFileDetails(materialFilename, content, txtSRV.Text.ToString().Trim());
}
if (fuUploadPurchaseOrder.HasFile)
{
string poFilename = Path.GetFileName(fuUploadPurchaseOrder.PostedFile.FileName);
Stream fs = fuUploadPurchaseOrder.PostedFile.InputStream;
int filesize = fuUploadPurchaseOrder.PostedFile.ContentLength;
BinaryReader br = new BinaryReader(fs);
byte[] content = br.ReadBytes(filesize);
Objects.UpdatePurchaseOrderFileDetails(poFilename, content, txtPurchaseOrderNo.Text.ToString().Trim());
}
}
现在在调试时,虽然我选择了一个文件,但fuUploadSRV.HasFile,fuUploadMaterialCertificate.HasFile,fuUploadPurchaseOrder.HasFile总是返回false。
我错过了什么吗?
答案 0 :(得分:1)
您是否尝试更新<form runat="server"
以包含enctype="multipart/form-data"
?
没有它可能会发生奇怪的事情!
答案 1 :(得分:0)
文件上传控件位于更新面板中。当我删除更新面板时,控件按预期工作。但是我明白了更新小组是如何发挥作用的,但是删除它已经解决了我的问题。