开发者

How to upload file to server from HTML Post ASP.NET

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.");
   开发者_JAVA技巧             }
                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.


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)


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜