FileUpload.hasfile始终为false [英] FileUpload.hasfile always false

查看:255
本文介绍了FileUpload.hasfile始终为false的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的页面上使用了FileUpload控件。即使选择了一个文件,它也会将FileUpload Control的hasfile属性显示为false.plz帮助...







I used FileUpload control on my page.Even after choosing a file ,it shows FileUpload Control's "hasfile" property as false.plz help...



<asp:UpdatePanel ID="updatePanel" runat="server" UpdateMode="Conditional">
    <ContentTemplate>       

<asp:fileupload id="fileupload" runat="server">
    </ContentTemplate>
</asp:UpdatePanel>







代码背后




code behind

void btnSave_Click(object sender, EventArgs e)
{
  if (fileupload.HasFile)
    {     
      fileupload.SaveAs(Server.MapPath(FileVirtualPath + fileupload.FileName));
     }
  Save();
}

推荐答案

为您的UpdatePanel添加触发器

<触发器>

< asp:PostBackTrigger ControlID =" btnSave" />

< /触发器>



单击上传按钮时会强制回发。



同时将以下行添加到Page_Load

Page.Form.Attributes.Add(" enctype"," multipart / form-data");
Add a trigger for your UpdatePanel
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>

This will force a postback when the upload button is clicked.

Also add the line below to the Page_Load
Page.Form.Attributes.Add("enctype", "multipart/form-data");


我在我的项目中这样做,它工作正常。



I am doing like this in my project and it is working fine.

<asp:UpdatePanel ID="UpdatePanel" runat="server">
   <ContentTemplate>
     <asp:FileUpload ID="uploadProfilePic" runat="server" ClientIDMode="Static"/>





代码落后 -





code behind -

HttpPostedFile objHttpPostedFile = uploadProfilePic.PostedFile;
string ProfilePic_fileName = uploadProfilePic.PostedFile.FileName;





尝试使用这个....



Try using this....


很难说究竟发生了什么,因为你仍然没有显示代码的所有相关部分。重要的是,您的代码示例没有说明按钮,该按钮被称为 btnSave ,并且没有任何内容表明事件处理程序 btnSave_Click 是添加到事件实例的调用列表单击此按钮的。是否在此按钮中未显示与文件的帖子相关的信息。重要的是,你没有显示表格及其控件。



所以,我只能怀疑这个问题与这个按钮有关和事件处理程序:此事件处理程序中发生的事情在发布文件之前发生,或文件未发布



如果您查看Microsoft代码示例,一切都很清楚:显示表单,可以看到触发帖子的内容。您也应该以类似的方式执行此操作,请参阅: http ://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile.aspx [ ^ ]。



-SA
It's hard to say what exactly happened, because you still don't show all relevant parts of the code. Importantly, your code sample tells nothing about the button which is supposedly named btnSave and nothing indicates that an event handler btnSave_Click is added to an invocation list of the event instance Click of this button. Is is not shown in this button related to the post of the file or not. Importantly, you don't show the form and its controls.

So, I only can suspect that the problem is related to this button and the event handler: what happens in this event handler happens before posting of the file, or file is not posted at all.

If you look at the Microsoft code sample, everything is clear: forms is shown, one can see what triggers the post. You should also do it in a similar way, please see: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.fileupload.hasfile.aspx[^].

—SA


这篇关于FileUpload.hasfile始终为false的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆