简体   繁体   中英

Unable to upload file by asp:FileUpload in asp.net 3.5 with c# 2.0

I am unable to upload file by asp:FileUpload always the FileUpload1.HasFile is false following are my code which i try to upload file but unfortunately still I am unable to upload file

ASPX Code

code behind c# code

protected void btSave_Click(object sender, EventArgs e)
{
    try
    {
            noteFile = "";
            /*File is existed or not cheked**/
            if (FileUpload1.HasFile)
            {
                /*File Size Checked*/
                if (FileUpload1.FileBytes.Length < 1024 * 1000)
                {
                    /*File Type Checked*/
                    string fileType=Path.GetExtension(FileUpload1.FileName);
                    if (fileType == ".xls" || fileType == ".doc")
                    {
                        /*get the last 'noteID' and add 1 to noteID*/
                        dt.Clear();
                        dt = dConnect.noteInfo(0, "", "", "", "", "", "", "", "admin");
                        noteID = 0;

                        /*Check the file name if any singal gile is save then need to delete it*/
                        dt.Clear();
                        dt = dConnect.noteInfo(0, "", "", "", "", "", "", dt.Rows[0]["noteID"].ToString() + '_', "byAdminFile");
                        exceprionString = "";
                        exceprionString = dConnect.exceptionMessage();

                        if (dt.Rows.Count == 0 && exceprionString.Equals(""))
                        {
                            noteID = 1 + Convert.ToInt32(dt.Rows[0]["noteID"].ToString());

                            FileUpload1.SaveAs(Server.MapPath("~/note") + noteID.ToString() + '_' + FileUpload1.FileName);

                            noteFile = noteID.ToString() + '_' + FileUpload1.FileName;
                        }
                        else
                        {
                            noteFile = "";
                            SMS("Only One File Can Stor Per Note");
                        }
                    }
                    else
                        SMS("Only Word or Excel File Can Upload");

                }
                else
                    SMS("File Size Should Not More Than 1 MB");
            }
            else
            {
                noteFile = "";
                SMS(FileUpload1.FileName.ToString());
            }

    }
    catch (Exception ex)
    {
        throw ex;
    }
}      

Are you uploading file using Browse ? If yes do you have anything on protected void Page_Load?

Are you using ASP.NET's "AJAX" if yes check this FileUpload1.HasFile is always returning false

Please check if your file contains some data. I was facing the same issue with the blank file. I added some text in the blank file and it resolved my issue.

And to check the filesize you can try using below code

 if(FileUpload1.PostedFile.ContentLength > 1048576);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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