繁体   English   中英

文件上传控制问题

[英]file upload control problem

我在尝试获取文件时在服务器端使用文件上传控制,这表明没有文件存在

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

我得到一个空字符串作为文件扩展名,即使选择了文件,upldDocument.HasFile也返回false。 可能是什么原因??

根据发布的代码,我只能提供最佳猜测。 没有足够的代码来确定问题出在哪里,但这是我的最佳猜测:

如果还没有,则需要检查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.
}

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

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

暂无
暂无

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

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