Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何检查FileUpload控件是否为空?_C#_Asp.net_File Upload - Fatal编程技术网

C# 如何检查FileUpload控件是否为空?

C# 如何检查FileUpload控件是否为空?,c#,asp.net,file-upload,C#,Asp.net,File Upload,我使用LINQ从数据库中检索数据,相对于链接的变量名是“service” upDocument是FileUpload控件的Id 目标是在上载新文件之前删除旧文件。 这是我想出的代码: if ((service.image_url != null || service.image_url != "") && (upDocument.FileName.Length != 0 || upDocument.PostedFile.ToString() != "")) {

我使用LINQ从数据库中检索数据,相对于链接的变量名是“service”

upDocument是FileUpload控件的Id

目标是在上载新文件之前删除旧文件。 这是我想出的代码:

if ((service.image_url != null || service.image_url != "") &&
    (upDocument.FileName.Length != 0 || upDocument.PostedFile.ToString() != ""))
{
     if (File.Exists(System.Web.HttpContext.Current.Server.MapPath(service.image_url)))
     {
          File.Delete(System.Web.HttpContext.Current.Server.MapPath(service.image_url));
     }
}
我遇到的问题是,尽管没有任何内容加载到文件上载,但文件仍在被删除。我做了一个断点并检查了它。。。与我期望的相反,FileName.Length不是0,postedFile.ToString()不是“”

如何进行正确的验证?

检查属性,如下所示:

if (!String.IsNullOrEmpty(service.image_url) && upDocument.HasFile) { 
    if (File.Exists(Server.MapPath(service.image_url)))
        File.Delete(Server.MapPath(service.image_url));
}
顺便说一句,在ASP.Net页面中,您不需要编写
System.Web.HttpContext.Current
,因此只需编写
Server.MapPath
,检查属性,如下所示:

if (!String.IsNullOrEmpty(service.image_url) && upDocument.HasFile) { 
    if (File.Exists(Server.MapPath(service.image_url)))
        File.Delete(Server.MapPath(service.image_url));
}

顺便说一句,在ASP.Net页面中,您不需要编写
System.Web.HttpContext.Current
,因此您可以简单地编写
Server.MapPath

如果
service.image\u url
null
或者
,您的问题是您正在删除文件。该条件始终为真,因为它不能同时为
null
“”


你应该写
!String.IsNullOrEmpty(service.image\u url)
,正如我在第一个答案中所写。

您的问题是,如果
服务,您将删除该文件。image\u url
null
或者
。该条件始终为真,因为它不能同时为
null
“”


你应该写
!String.IsNullOrEmpty(service.image\u url)
,正如我在第一个答案中所写。

您的第一个条件(
service.image\u url
)应该使用and(
&&&&
),而不是or(
|
)。但是,它实际上应该调用
String.IsNullOrEmpty
。顺便说一句,
upDocument.PostedFile.ToString()
将始终是
“HttpPostedFile”
,因为
HttpPostedFile
类不会重写
ToString
。因此,没有必要检查它。您应该改为选中HasFile
。您的第一个条件(
service.image\u url
)应该使用and(
&&&
),而不是or(
|
)。但是,它实际上应该调用
String.IsNullOrEmpty
。顺便说一句,
upDocument.PostedFile.ToString()
将始终是
“HttpPostedFile”
,因为
HttpPostedFile
类不会重写
ToString
。因此,没有必要检查它。您应该改为选中HasFile