c# - 为什么 file.PostedFile 总是 null

标签 c# html asp.net

我需要将图像上传到我的数据库,为此我需要将它们带到服务器端。 唯一的问题是该文件始终为空,我无法使用它。

html:

<form id="signUpForm" runat="server" action="signUp.aspx" method = "get" name = "signUp" 
enctype="multipart/form-data" onsubmit = "return validateSignUp();">
    <div id = "input">
    <table>
        <tr>
        <td class = "description">*profile image: </td>
        <td class = "input" id = "inputProfileImage"><input type = "file" name = "profileImage" accept = "image/png, image/jpeg, image/jpg" id = "profileImage" runat="server"/>
            <div class = "warning" id = "warnProfileImage"></div>

            </td>
        </tr>

        <tr>
        <td><input type = "submit" value = "sign up"/></td>
        <td><input type = "reset" value = "reset"/></td>
        </tr>

    </table>

    </div>

    <div id = "showInfo">
    <table>

        <tr><td class = "description">profile image:</td><td class = "input"><img src = "defaultProfileImages/defaultProfileImage1.png" id = "showProfileImage" name="showProfileImage" runat="server"/></td></tr>

        <tr><td><input type = "submit" name = "submit" value = "confirm"/></td></tr>

    </table>
    </div>
    </form>

c#:

if (Request.QueryString["submit"] != null)
    {
        string path = "";
        if ((profileImage.PostedFile != null) && (profileImage.PostedFile.ContentLength > 0))
        {
            string fn = System.IO.Path.GetFileName(profileImage.PostedFile.FileName);
            string SaveLocation = Server.MapPath("Temp") + "\\" + fn;
            path = SaveLocation;
            try
            {
                profileImage.PostedFile.SaveAs(SaveLocation);
                Response.Write("The file has been uploaded.");
            }
            catch (Exception ex)
            {
                Response.Write("Error: " + ex.Message);
                //Note: Exception.Message returns a detailed message that describes the current exception. 
                //For security reasons, we do not recommend that you return Exception.Message to end users in 
                //production environments. It would be better to put a generic error message. 
            }
        }
        User user = new User(Libary.convertFile(path));
        UserService userService = new UserService();
        userService.InsertUser(user);
        Response.Redirect("homePage.aspx");
        Response.End();
    }

*我删除了所有与文件无关的行。

最佳答案

尝试使用 HttpPostedFileBase 而不是 PostedFile 来快速解决问题。

关于c# - 为什么 file.PostedFile 总是 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60485599/

相关文章:

c# - 如果文本框为空,则假设变量 = 0 C#

c# - 如何在mvc3,C#中将数字显示到小数点后两位?

html - 固定页眉与页内 anchor 重叠

javascript - document.getElementById 不工作,即使我的 css 中存在 ID

c# - 用逗号和句点验证正数

javascript - 当我们在asp.net中有相同用户控件的多个实例时,如何获得正确的控件ID?

c# - 是否可以更改匿名类型?

c# - 为什么我的攻击函数会破坏所有对象包括攻击者?

javascript - 如何分析/减少 html 页面渲染时间?

c# - 向 asp.net 中的缓慢页面添加实时进度更新