我知道这个问题很常见,但就我而言,我涉及 JavaScript,所以这可能是问题所在。这就是我正在做的事情。我调用 asp FileUpload 来允许用户选择图像。然后通过调用 onchange 事件,我触发了一个 javascript,这反过来又使隐藏的 div 变得可见。 div 包含一个确认按钮,然后触发上传功能:
<div class="profile-page-owner-profile-pic">
<asp:FileUpload ID="profile_pic_input" onchange="profilenewimageinput(this)" runat="server"></asp:FileUpload>
</div>
隐藏分区:
<div id="profile-change-profile-pic-bg-overlay-div">
<div class="profile-change-profile-pic-bottom-bar">
<asp:Button ID="picApply" class="cropButton" runat="server" OnClick="picApply_Click" Text="Button" />
<span class="profile-page-cancel-crop-button" onclick="hideprofilepicwindow()">Cancel</span>
</div>
</div>
JS:
function profilenewimageinput() {
document.getElementById("profile-change-profile-pic-bg-overlay-div").style.display = "block";
setTimeout(function () {
document.getElementById("profile-change-profile-pic-bg-overlay-div").style.opacity = 1;
}, 100);
}
隐藏代码:
protected void picApply_Click(object sender, EventArgs e)
{
if (profile_pic_input.HasFile) //always returns false when debugged
{
//Code
}
}
为什么即使选择了图像,我的 HasFile 也总是返回 False?
最佳答案
尝试将其放入 <asp:UpdatePanel>
具有适当的Trigger
设置。以下可能有效,但我无法测试:
<div class="profile-page-owner-profile-pic">
<asp:UpdatePanel ID="UploadPanel" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="picApply" />
</Triggers>
<ContentTemplate>
<asp:FileUpload ID="profile_pic_input" onchange="profilenewimageinput(this)" runat="server" />
</ContentTemplate>
</UpdatePanel>
</div>
关于javascript - FileUpload HasFile 返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30713266/