开发者

FileUpload1.HasFile is always returning false

I am uploading file using ASP.net File upload control. My FileUpload1.HasFile is always returning false.

if (FileUpload1.HasFile)
        {
            DBOperations db = new DBOperations();

            try
            {
                FileUpload1.SaveAs(Server.MapPath("~/uploadedIm开发者_StackOverflowages/" + db.uploadImage(System.IO.Path.GetExtension(FileUpload1.FileName)) + System.IO.Path.GetExtension(FileUpload1.FileName)));
            }
            catch (Exception Ex)
            {
                String he = Ex.Message;
            }
        }

I am using following ASP.net Code

<asp:UpdatePanel ID="fileUpload" runat="server">
           <ContentTemplate>
             <Triggers>
                  <asp:AsyncPostBackTrigger ControlID="btnUploadFile" EventName="Click" />
             </Triggers>
               <asp:FileUpload ID="FileUpload1" runat="server" /> 
               <asp:Button ID="btnUploadFile" Text="Upload File" runat="server" 
                   onclick="btnUploadFile_Click" />
                   <br />
             <asp:RegularExpressionValidator ID="revImage" ControlToValidate="FileUpload1" ValidationExpression="^.*\.((j|J)(p|P)(e|E)?(g|G)|(g|G)(i|I)(f|F)|(p|P)(n|N)(g|G))$" Text="Invalid image type" runat="server" />

          </ContentTemplate>
       </asp:UpdatePanel>

I have tried even by removing AsyncPostBackTrigger and even by removing Whole asp:updatePanel then also my FileUpload1.HasFile always returns false.


ASP.NET's "AJAX" thing doesn't support file uploads in UpdatePanels out of the box. Change that trigger into a PostBackTrigger (which causes a full page load) or use something else to upload the file.


Add a trigger for your UpdatePanel

<Triggers> 
   <asp:PostBackTrigger ControlID="btnUploadFile" /> 
</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 know this post if old, but I found that if the file is empty [ 0 KB ] then it will return false as well. There has to be something in the file in order for .HasFile to acknowledge it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜