无法使用c#2.0在asp.net 3.5中通过asp:FileUpload上传文件

时间:2012-07-25 05:30:38

标签: c# asp.net

我无法通过asp上传文件:FileUpload总是FileUpload1.HasFile为false 以下是我尝试上传文件的代码,但遗憾的是我仍然无法上传文件

ASPX代码      

代码隐藏在c#代码

之后
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;
    }
}      

2 个答案:

答案 0 :(得分:0)

您使用浏览上传文件吗? 如果是,你对protected void Page_Load有什么了吗?

您使用的是ASP.NET的“AJAX”吗? 如果是,请检查此FileUpload1.HasFile is always returning false

答案 1 :(得分:0)

请检查您的文件是否包含某些数据。我在空白文件中遇到了同样的问题。我在空白文件中添加了一些文本,它解决了我的问题。

要检查文件大小,您可以尝试使用以下代码

 if(FileUpload1.PostedFile.ContentLength > 1048576);