简体   繁体   English

asp.net网站从C#代码后面调用输入文件

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

i have ASP web site with input file in it. 我有ASP网站,其中有输入文件。 i want to take this file with name from this input to my code behind but all time i am getting null for "FileUp1". 我想将此文件的名称从此输入输入到我的代码后面,但所有时间我都为“ 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 您的表单标签应具有enctype标签

    <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# C#背后的代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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