简体   繁体   中英

asp.net website calling input file from C# code behind

i have ASP web site with input file in it. i want to take this file with name from this input to my code behind but all time i am getting null for "FileUp1". this is my code code:

ASP:
<input type="file" id="File1" name="File1"/>

C#:
HttpPostedFile FileUp1 = Request.Files["File1"];

Your form tag should have enctype tag

    <form id="form1" runat="server" enctype="multipart/form-data">
....
</form>

Input type file should come under this form.

After that you can use same code which you are using currently.

Sample code

source page

<form id="form1" runat="server" enctype="multipart/form-data">
 <input type="file" id="myFile" name="myFile" />
 <asp:Button runat="server" ID="btnUpload" OnClick="btnUploadClick" Text="Upload" />
</form>

Code behind C#

 protected void btnUploadClick(object sender, EventArgs e)
{
    HttpPostedFile file = Request.Files["myFile"];
    if (file != null && file.ContentLength > 0)
    {
        //do your stuff
    }
}

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