简体   繁体   中英

asp.net C# Fileupload always returns false

I am working with a fileuploader to upload a picture. However, evertyime i select the file and click the upload button it says that the fileuploader.file returns a value of false and does not run the next lines of code. All i can find is that it has to be in in a seperate form and that the method has to be "post", but that did not fix the problem.

here is my codebehind.

 if (FileUploadControl.HasFile)
        {
            try
            {
                string filename = Path.GetFileName(FileUploadControl.FileName);
                FileUploadControl.SaveAs(Server.MapPath("~/") + filename);
                StatusLabel.Text = "Upload status: File uploaded!";
            }
            catch (Exception ex)
            {
                StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }

here is my front end code.

                    <form id="form2" action="CompProfile.aspx" method="post" enctype="multipart/form-data">
                    <asp:FileUpload id="FileUploadControl" runat="server" />
                    <asp:Button runat="server" id="UploadButton" text="Upload" onclick="UploadButton_Click" />
                    <asp:Label runat="server" id="StatusLabel" text="Upload status: " />
                    </form>

If you are testing with a 0 byte sized file it will return false. Make sure the file actually isn't empty.

您的表单应具有runat="server"而不是method="post"action="..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM