gpt4 book ai didi

c# - 为什么我的文件上传控件不能正常工作

转载 作者:行者123 更新时间:2023-11-30 21:06:02 24 4
gpt4 key购买 nike

我的 asp.net 页面中有一个 FileUpload 控件,它应该将我的图片上传到主机上的某个文件夹中。我设置了一个条件,即当它有文件时,它开始上传。这是代码。

 if (File2.HasFile)
{
string b = File2.FileName.ToString();
File2.PostedFile.SaveAs(path + File2.FileName);
}

但条件永远不会为真。我在该页面中有另外两个 FileUpload。它们工作正常。但是这个工作不正常,条件始终为假。我该怎么办?

这是 HTML 代码:

<asp:UpdatePanel ID="UpdatePanel3" runat="server">
<ContentTemplate>
<table cellpadding="0" cellspacing="0" class="style4" dir="rtl"
style="border: thin solid #008080">
<tr>
<td>
&nbsp;</td>
<td>
&nbsp;</td>
</tr>
<tr>
<td>
<div style=" border-width: thin; border-color: #008080; border-left-style: solid;">
<asp:GridView ID="GridView2" runat="server" AllowPaging="True"
AutoGenerateColumns="False" CellPadding="4" DataSourceID="SqlDataSource4"
ForeColor="#333333" GridLines="None" onrowcommand="GridView2_RowCommand"
PageSize="6" Width="130px" Height="200px" ShowHeader="False">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CommandArgument='<%#Eval("PID") %>' CommandName="LnkEdit"
Text='<%#Eval("Pname") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#7C6F57" />
<FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#E3EAEB" />
<SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F8FAFA" />
<SortedAscendingHeaderStyle BackColor="#246B61" />
<SortedDescendingCellStyle BackColor="#D4DFE1" />
<SortedDescendingHeaderStyle BackColor="#15524A" />
</asp:GridView>
</div>
<asp:Label ID="Label18" runat="server"></asp:Label>
</td>
<td>
<div style="float :right">
<table cellpadding="0" cellspacing="0" style="width: 500px">
<tr>
<td>
<asp:Label ID="Label1" runat="server" Text="نام"></asp:Label>
</td>
<td align="right">
<asp:TextBox ID="TxTEditname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label15" runat="server" Text="زبان"></asp:Label>
</td>
<td align="right">
<asp:DropDownList ID="DropDownList4" runat="server">
<asp:ListItem Value="en">English</asp:ListItem>
<asp:ListItem Value="fa">فارسی</asp:ListItem>
</asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text="توضیحات"></asp:Label>
</td>
<td align="right">
<asp:TextBox ID="TxTEditdes" runat="server" Height="102px"
TextMode="MultiLine" Width="380px"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Label ID="Label19" runat="server" Text="عکس جدید"></asp:Label>
</td>
<td align="right">
<asp:FileUpload ID="File2" runat="server" />
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click"
Text="ثبت تغییرات" />
</td>
<td>
<asp:Label ID="Label17" runat="server"></asp:Label>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
</ContentTemplate>




</asp:UpdatePanel>

最佳答案

来自 MSDN页:

The following ASP.NET controls are not compatible with partial-page updates, and are therefore not designed to work inside an UpdatePanel control:

  • FileUpload and HtmlInputFile controls when they are used to upload files as part of an asynchronous postback.

  • -

To use a FileUpload or HtmlInputFile control inside an UpdatePanel control, set the postback control that submits the file to be a PostBackTrigger control for the panel.

因此您需要为回发控件(按钮等)设置 Triggers 属性

例子:

<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="Button1" />
</Triggers>
</asp:UpdatePanel>

代码隐藏

protected void Button1_Click(object sender, EventArgs e)
{
if(FileUpload1.HasFile)
{
FileUpload1.SaveAs(MapPath("~/Files/" + FileUpload1.FileName));
}
}

关于c# - 为什么我的文件上传控件不能正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11410088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com