neriazadok
neriazadok

Reputation: 43

why is file.PostedFile is always null

I need to upload images to my data base, and in order to that I need to bring them to the server side. The only problem is the file is always null and I can't use it.

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();
    }

*I deleted all the lines that doesn't has anything to do with the file.

Upvotes: 2

Views: 143

Answers (2)

neriazadok
neriazadok

Reputation: 43

Turn out you can't get a file in the server side if you are using a "get" method in your form. So you just need to change the form method to "post".

HTML:

<form id="signUpForm" runat="server" action="signUp.aspx" method = "post" name = "signUp" enctype="multipart/form-data" onsubmit = "return validateSignUp();">
    <table>
        <tr>
        <td class = "description">phone number: </td>
        <td class = "input" id = "inputPhoneNumber"><input type = "tel" pattern="[0-9]{3}-[0-9]{7}" name = "phoneNumber" id = "phoneNumber" runat="server"/><div class = "warning" id = "warnPhoneNumber"></div></td>
        </tr>

        <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>
    </form>

You just need to know that you can't use "Request" if your from method is "post", So I just used one of the others input's in the form.

C#:

using System.Drawing;
using System.IO;

public partial class signUp : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if ((phoneNumber.Value != null) && (phoneNumber.Value.Length > 0))
        {
            string path = "";
            bool deleteFile = false;
            if ((profileImage.PostedFile != null) && (profileImage.PostedFile.ContentLength > 0))
            {
                path = Libary.save(profileImage.PostedFile, Server);
                deleteFile = true;
            }
            User user = new User(Libary.convertFile(path));
            UserService userService = new UserService();
            userService.InsertUser(user);
            Response.Redirect("homePage.aspx");
            Response.End();
            if ((deleteFile) && (File.Exists(path))){File.Delete(path);}
        }
    }
}

*Libary.save(HttpPostedFile file, HttpServerUtility Server) function is just saving the file and returning the path to it.

Upvotes: 1

Ravi Khunt
Ravi Khunt

Reputation: 262

Try to use HttpPostedFileBase instead of PostedFile for quick issue resolve.

Upvotes: 1

Related Questions