FileUpload.hasFile is always False

2019-01-14 05:52发布

I have a FileUpload control (and it's not inside an UpdatePanel) and its hasFile property is always False.

   <asp:FileUpload ID="certificateUploader" runat="server"/>

Any thought?

7条回答
ら.Afraid
2楼-- · 2019-01-14 06:11

You cannot upload files using AJAX => you should not be placing a FileUpload control inside an UpdatePanel because this UpdatePanel sends an AJAX request to the server.

查看更多
兄弟一词,经得起流年.
3楼-- · 2019-01-14 06:13

Add a trigger for your UpdatePanel

<Triggers>
   <asp:PostBackTrigger ControlID="btnCertificateUpload" />
</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");
查看更多
闹够了就滚
4楼-- · 2019-01-14 06:17

I also uploaded a file using the FileUpload control, but the HasFile property returned false. Turn out that FileUpload.HasFile is also false if you upload an empty file. In this case adding some text to the file you want to upload will make the Hasfile property return true.

查看更多
Rolldiameter
5楼-- · 2019-01-14 06:18

the whole time it was about the permissions i had(or didn't have to be more specific) over the file am trying to upload, i granted the user the sufficient permissions and it all went well.

thanks a lot for your help and posts.

查看更多
6楼-- · 2019-01-14 06:21

To complement the example given by @dbFrameIT Support:

        <asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Always" runat="server">
            <ContentTemplate>
                <asp:FileUpload ID="FileUpload1" runat="server" />
                <asp:Button ID="UploadButton" runat="server" Text="Upload Selected File" OnClick="UploadButton_Click" />
                <asp:Label ID="UploadDetails" runat="server" Text=""></asp:Label>
            </ContentTemplate>
            <Triggers>
                <asp:PostBackTrigger ControlID="UploadButton" />
            </Triggers>
        </asp:UpdatePanel>

your code behind (c#)

    protected void UploadButton_Click(object sender, EventArgs e)
    {
        if (FileUpload1.HasFile == false)
        {
            UploadDetails.Text = "Please first select a file to upload...";
        }
        else
        {
            string FileName = FileUpload1.FileName;
            UploadDetails.Text = string.Format(
                    @"Uploaded file: {0}<br />
              File size (in bytes): {1:N0}<br />
              Content-type: {2}",
                      FileName,
                      FileUpload1.FileBytes.Length,
                      FileUpload1.PostedFile.ContentType);

            // Save the file
            string filePath = Server.MapPath("~/Brochures/" + FileUpload1.FileName);
            FileUpload1.SaveAs(filePath);
        }
    }
查看更多
\"骚年 ilove
7楼-- · 2019-01-14 06:31

Sometimes fileUpload has problems. You can use simple input:

<input id="filMyFile" type="file" runat="server"></input>

In code save file to server:

HttpPostedFile myFile = filMyFile.PostedFile;           
string fullPath=Server.MapPath("~/UploadDocuments/") + myFile.FileName;
myFile.SaveAs(fullPath);

And file will save at UploadDocuments folder in your ASP.NET application (server)

查看更多
登录 后发表回答