单个文件上载检查ASP.NET

时间:2012-08-13 08:23:03

标签: asp.net file upload

成功传输代码传输文件,但不会在标签中打印已传输的文件。尽管其他检查也很完美。它有任何逻辑错误吗?

        if (txtFile1.HasFile)
        {
        var folderfile1 = Server.MapPath("~/files/Operations_Support/IMSI");
        string flFile1 = txtFile1.PostedFile.FileName;
        string saveflFile1 = folderfile1 + "\\" + System.IO.Path.GetFileName(flFile1);

        FileInfo Finfo = new FileInfo(txtFile1.PostedFile.FileName);
        if (Finfo.Extension.ToLower() == ".txt")
        {
            if (txtFile1.PostedFile.ContentLength < 1024)
            {
                //if (!File.Exists(folderfile1))
                if (Directory.GetFiles(folderfile1).Length == 0)
                {
                    txtFile1.SaveAs(saveflFile1);
                    //lbFile1.Visible = true;
                    lbFile1.Text = "Upload status: IMSI file transfered successfully";
                }

                else
                {
                    lbFile1.Text = "Upload status: Please wait until the previous file is processed";
                }

            }
            else
                lbFile1.Text = "Upload status: The file has to be less than 1 kb!";
        }
        else
            lbFile1.Text = "Upload status: Only Text files are accepted!";
    }
    else
    {
        lbFile1.Visible = true;
        lbFile1.Text = "Upload status: Please select file";
    }

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

像这样使用File.Exists

var folderfile4 = Server.MapPath("~/files/Path");
string saveflFile4 = folderfile4 + "\\" + System.IO.Path.GetFileName(flFile4);
if(!File.Exists(saveflFile4))
    txtFile4.SaveAs(saveflFile4);
else
    txtError.Text = "File exists";

Server.MapPath返回服务器上的物理路径,因此您可以使用File.Exists检查文件是否存在。