Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/37.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
Asp.net 文件上载控制问题_Asp.net_File Upload - Fatal编程技术网

Asp.net 文件上载控制问题

Asp.net 文件上载控制问题,asp.net,file-upload,Asp.net,File Upload,我在服务器端使用文件上传控制,当我试图获取文件时,它显示没有文件存在 <asp:FileUpload ID="upldDocument" runat="server" /> string fileExtension = System.IO.Path.GetExtension(upldDocument.FileName); if (upldDocument.HasFile) { } 字符串fileExtension=System.IO.Path.GetExtension(upldD

我在服务器端使用文件上传控制,当我试图获取文件时,它显示没有文件存在

<asp:FileUpload ID="upldDocument" runat="server" />
string fileExtension = System.IO.Path.GetExtension(upldDocument.FileName);
if (upldDocument.HasFile)
{
}

字符串fileExtension=System.IO.Path.GetExtension(upldDocument.FileName);
if(upldDocument.HasFile)
{
}
我得到一个空字符串作为文件扩展名,upldDocument.HasFile即使在选择文件后也返回false。
原因可能是什么???

您如何检查这些值?(在什么情况下)


您是否将表单的enctype属性设置为“multipart/form data”?

根据发布的代码,我只能给出一个最佳猜测。没有发布足够的代码来确定问题是什么,但下面是我的最佳猜测:

如果还没有,则需要检查HasFile属性

有关完整示例,请参见:

编辑-添加

在错误代码之后使用HasFile不会有帮助。您需要将获取扩展名的代码放在if语句中,以便它仅在存在文件时尝试读取扩展名

string fileExtension = "";
if (upldDocument.HasFile)
{
  fileExtension  = System.IO.Path.GetExtension(upldDocument.FileName);

}
else
{
   //No file selected by user, which is why you can't get the extension.
   // handle this eventuality here even if it means just returning from the function and not doing anything.
}