0

I am trying to upload a file to server from html post.

<form action="insertBlogCat.aspx" method="post" enctype="multipart/form-data">

        <input id="uploader" name="userfile" type="file" />

        <br /><br />

        <input type="submit" value="Upload" id="pxUpload" />
        <input type="reset" value="Clear" id="pxClear" />
    </form>

My server side code:

protected System.Web.UI.HtmlControls.HtmlInputFile CatBlogImgFile;
 private void Insert()
        {

            if ((CatBlogImgFile.PostedFile != null) && (CatBlogImgFile.PostedFile.ContentLength > 0))
            {
                string fn = System.IO.Path.GetFileName(CatBlogImgFile.PostedFile.FileName);
                string SaveLocation = Server.MapPath("Data") + "\\" + fn;
                try
                {
                    CatBlogImgFile.PostedFile.SaveAs(SaveLocation);
                    Response.Write("The file has been uploaded.");
                }
                catch (Exception ex)
                {


                }
            }
            else
            {
                Response.Write("Please select a file to upload.");
            }
        }

But CatBlogImgFile.PostedFile value is always null after posting from HTML file. PLease help. Thanks in advance.

2 Answers 2

2

You can't mix normal HTML elements and server-side controls like that.

Either use ASP.Net WebControls in the ASPX page (<asp:FileUpload />), or use the Request object directly and don't use server-side controls at all (Request.Files)

Sign up to request clarification or add additional context in comments.

Comments

0

You may want to see if the Request.Files collection has any content. Normally in WebForms, you'd use a different control for doing uploads.

You may also want to look over the MSDN article: How to: Add HTML Server Controls to a Web Page Using ASP.NET Syntax . The main thing you're missing is the runat="server" part.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.