简体   繁体   English

无法使用C#2.0在ASP.NET 3.5中通过ASP:FileUpload上传文件

[英]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 我无法通过asp上传文件:FileUpload始终FileUpload1.HasFile为false,以下是我尝试上传文件的代码,但不幸的是我仍然无法上传文件

ASPX Code ASPX代码

code behind c# code 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;
    }
}      

Are you uploading file using Browse ? 您是否正在使用“浏览”上传文件? If yes do you have anything on protected void Page_Load? 如果是,您在受保护的无效Page_Load上有任何东西吗?

Are you using ASP.NET's "AJAX" if yes check this FileUpload1.HasFile is always returning false 如果使用的是ASP.NET的“ AJAX” ,请检查此FileUpload1.HasFile始终返回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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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